String to Date in Android [closed] - android

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
i have a string which means a date. it looks like this :
/Date(1448880305230+0200)/
So how to convert it to Calendar or any type of date ?

The string above in your question doesn't look like unixtime, but technically speaking you can use SimlpeDateFormat to format your date like this:
long unixSeconds = 1372339860;
// *1000 is to convert seconds to milliseconds
Date date = new Date(unixSeconds*1000L);
// the format of your date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
String formattedDate = sdf.format(date);
//Log it in
//System.out.println(formattedDate);
//Log.d(formattedDate);

Related

Convert a String to each device Date Format [duplicate]

This question already has answers here:
Date formatting based on user locale on android
(8 answers)
Closed 4 years ago.
I have a string like this "2018-08-30 03:45:39". Now I want to show it on view depend on each devide's datetime formats. Ex: My phone's datetime format is dd/mm/yyyy, my friend's phone is mm/dd/yyy. Can someone help me?
You have to use toLocalizedPattern() method
Try the following code:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
String dateLocalizedFormatPattern = simpleDateFormat.toLocalizedPattern();
Log.d("DatePattern",dateLocalizedFormatPattern )

Getting system date in low level API 17 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I need a way to get system date,to display in my mobile app. Minimum API level of 17 without using calendar class.
You can use the following code to get System Date, Time, TimeZone :
Date currentDate = new Date(System.currentTimeMillis());
Toast.makeText(YourActivity.this, currentDate.toString(), Toast.LENGTH_SHORT).show();
Then SPLIT your string to get DATE, TIME separately:
Hope this helps.
Get system date with Calendar class:
Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();
Get system date with Date class:
Date date = new Date();
All this classes are exist in API level 17 and higher.

Android Current Time [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I want to show current time of device in app. following is the code is getting time but problem is if current time is 12:15 but my variable cTime has opposite time that is 00:15. Below is my code for getting time, please help me out
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
Calendar c = Calendar.getInstance();
int currentHour = c.get(Calendar.HOUR);
int currentMin = c.get(Calendar.MINUTE);
String currentTime = currentHour+":"+currentMin;
Date cTime = sdf.parse(currentTime);
You should use Calendar.HOUR_OF_DAY instead of Calendar.HOUR to get the hour in 0-24 format.
Try this Code For current time
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("h:mm aa");
String datetime = sdf.format(now);
Use this simple method for get device current time in milisecound
System.currentTimeMillis();
Always, always read the documentation for the code you're using.
You are formatting your hours with the H pattern, which describes the 24 hour model:
H | Hour in day | (0-23) | Number | 0
You need to use the small h, which represents the “Hour in am/pm (1-12)”:
SimpleDateFormat format = new SimpleDateFormat("hh:mm");
Date date = new Date();
String dateReadable = format.format(date);
// Use your String elsewhere.
And this is all you need to get started. No need to create ints and Calendars for this. You can also put the a for the period indicator, as Arjun said below.
This is the foundation for this answer, too, but considering the code is slightly different and you are facing difficulties (by looking at your code), I didn't consider it a duplicate.

How to get my device date [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to get my android device date and check with current date and compare number of days left. Tab or device doesn't have SIM also.
Thanks!
You can use this code to get the device date.
SimpleDateFormat s = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss", Locale.getDefault());
String dateFormat = s.format(new Date(System.currentTimeMillis()));
Android API for managing times android.text.format.Time
For getting current time:
Time time = new Time();
time.setToNow();
To calculate differences between two time, you can try this code
Time time1;
Time time2;
long diff = time1.toMillis(false) - time2.toMillis(false);
// Now 'diff' contains difference between those two time in milliseconds
Update #1
This class was deprecated in API level 22.

Converting string "ex: PT20H" to time in android [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm developing an android application.
This time format returns to me as a string from the database -->"PT20H" and I want to convert it to the following format --> "20:00".
Any ideas??
Thanks in advance.
What you want to do is called parsing a time string. Take a look at SimpleDateFormat
Its seems like you've got some plain text that isn't informative, being the PT and the H. That is, if all your times are whole hours only (eg "PT20H23M" for 20:23 does not occur. I believe the pattern to use should be "'PT'H'H'"
That is PT (between quotes to signify text to ignore, then the hour (symbol H) and then H between quotes again because it is to be ignored.
SimpleDateFormat sdf = new SimpleDateFormat("'PT'H'H'");
Date theDate = sdf.parse("PT20H");
SimpleDateFormat newFormat = new SimpleDateFormat("H:m");
String newFormattedString = newFormat.format(theDate);

Categories

Resources