What is wrong with my date format? - android

I am trying to get the date in this format "time":"05:09pm 08/02/2011". What I have so far is
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(
"HH:mmaa MM/dd/yyyy");
holder.put("time", sdf.format(c.getTime()));
and this is what comes out.
"time":"21:28PM 08\/26\/2011"
Why is this happening and what can I do to fix it? Thanks

I suspect you want a format of
hh:mmaa MM/dd/yyyy
As hh is the 12-hour format in the range 01-12. I find that's typically how humans represent 12-hour values. Of course if you want 00-11, use KK instead as suggested by MByD.
(Quite what the rationale is for the capitalized form being the 24-hour version of HH, but the lower case form being the 24-hour version of kk, I don't know...)
If you were worried by the backslashes in the output, I suspect that's just JSON-escaping. I'm surprised it's necessary for forward-slashes, but it shouldn't do any harm.

Try changing HH:mmaa MM/dd/yyyy to KK:mmaa MM/dd/yyyy. (KK is hour in am/pm, 0-11)

Related

Convert indian time zone to local time

In my app I am getting time from server in API in IST timezone, I want to show time in device's local time zone.
Below is my code for this but it seems its not working.
SimpleDateFormat serverSDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat utcSDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat localSDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
serverSDF.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));
utcSDF.setTimeZone(TimeZone.getTimeZone("UTC"));
localSDF.setTimeZone(TimeZone.getDefault());
Date serverDate = serverSDF.parse(dateString);
String utcDate = utcSDF.format(serverDate);
Date localDate = localSDF.parse(utcDate);
From server I am getting time "2018-02-28 16:04:12" in IST and the code above displays "Wed Feb 28 10:34:12 GMT+05:30 2018".
The other answer uses GMT+05:30, but it's much better to use a proper timezone such as Asia/Kolkata. It works now because India currently uses the +05:30 offset, but it's not guaranteed to be the same forever.
If someday the government decides to change the country's offset (which already happened in the past), your code with a hardcoded GMT+05:30 will stop working - but a code with Asia/Kolkata (and a JVM with the timezone data updated) will keep working.
But today there's a better API to manipulate dates, see here how to configure it: How to use ThreeTenABP in Android Project
This is better than SimpleDateFormat, a class known to have tons of problems: https://eyalsch.wordpress.com/2009/05/29/sdf/
With this API, the code would be:
String serverDate = "2018-02-28 16:04:12";
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime istLocalDate = LocalDateTime.parse(serverDate, fmt);
// set the date to India timezone
String output = istLocalDate.atZone(ZoneId.of("Asia/Kolkata"))
// convert to device's zone
.withZoneSameInstant(ZoneId.systemDefault())
// format
.format(fmt);
In my machine, the output is 2018-02-28 07:34:12 (it varies according to the default timezone of your environment).
Although it seems complicated to learn a new API, in this case I think it's totally worth it. The new API is much better, easier to use (once you learn the concepts), less error-prone, and fix lots of problems of the old API.
Check Oracle's tutorial to learn more about it: https://docs.oracle.com/javase/tutorial/datetime/
Update: Check this answer by #istt which uses modern Java8 date-time api.
You don't need to change format in UTC first. You can simply use:
SimpleDateFormat serverSDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat localSDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
serverSDF.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
localSDF.setTimeZone(TimeZone.getDefault());
String localDate = localSDF.format(serverSDF.parse(dateString));

Android 12 hours Time Format getting wrong

I am using
DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy hh:mm a");
String dateAsString = dateFormat.format(gmt);
And getting String 06-06-2017 08:15 a.m.
Why I am getting a.m. instated of AM or PM?
The AM/PM/a.m. actually depends on the device. Try the same code on other devices and you might get to see a different result. If you need AM/PM only, then you need to do it manually by replacing the dots and converting it to uppercase.
It depends on the locale. If you use SimpleDateFormat (which you may not want to do, see below), I recommend you give it an explicit locale. The one you construct uses the device’s default, which explains why you get different results on different devices. If you want that, use new SimpleDateFormat("MM-dd-yyyy hh:mm a", Locale.getDefault()) so the reader knows you have thought about it. To make sure you get AM and PM, use for example new SimpleDateFormat("MM-dd-yyyy hh:mm a", Locale.ENGLISH).
Why would you not want to use SimpleDateFormat? I consider it long outdated since the much better replacement for the Java 1.0 and 1.1 classes came out with Java 8 in 2014. They have also been backported to Android Java 7 in the ThreeTenABP. Get this and write for example:
LocalDateTime gmt = LocalDateTime.of(2017, Month.JUNE, 6, 8, 15);
DateTimeFormatter dateTimeFormat = DateTimeFormatter.ofPattern("MM-dd-uuuu hh:mm a",
Locale.ENGLISH);
String dateAsString = gmt.format(dateTimeFormat);
The result is
06-06-2017 08:15 AM
To make explicit that the time is in GMT, you may use an OffsetDateTime with offset ZoneOffset.UTC.
Link: How to use ThreeTenABP in Android Project.

MediaStore.Video.Media.DATE_MODIFIED format

Can anyone show me how to format the MediaStore.Video.Media.DATE_MODIFIED to the human readable date format. I read in the docs that the time is in seconds. So i simply multiply it by 1000 and use the SimpleDateFormat . Here is my code
Date d=new Date(mills*1000);
DateFormat df=new SimpleDateFormat("dd-mm-yyyy");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
return df.format(d);
This returns a String 25-35-2012 that off course is not correct. Any help with this ?
Kind Regards
you should use capital M, m refers to minute in hour while M refer to Month in year.
see this for further info

Conversion of Epoch Date to Human Readable format in android

I have a problem that I am having an Epoch date which is coming from Web Services, I want to display it in Human Readable format as, July 12, 2012, but my code always shows 16/01/1970 for any Epoch Date. I don't know where I am Wrong or How to convert Epoch Date to Date in Java. Please suggest me any solution regarding the same.
Code:
Date.setText(new java.text.SimpleDateFormat("dd/MM/yyyy").format(new java.util.Date(Long.parseLong(newsDetail.getDate_Posted()))));
Thanks in advance.
I have a hunch that Long.parseLong(newsDetail.getDate_Posted()) is returning seconds from epoch not milliseconds.
If you always see 16/01/1970, then Long.parseLong(newsDetail.getDate_Posted()) is between 1296000000 and 1382400000. It turns out that if I multiple these minimum and maximum number by 1000, I see dates between 26/1/2011 and 21/10/2013. I'm guessing that the date you are expecting falls in this new range.
Try using:
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
date.setText(sdf.format(new Date(Long.parseLong(newsDetail.getDate_Posted()) * 1000)));

How do I get the time in this format in android?

I am trying to get the present time in this format in an android app. time= "05:09pm 08/02/2011" Right now I am using Calendar c=Calendar.getInstance() and c.getTime() to get the time and its coming out as Tue Aug 23 02:34:25 PDT 2011.
Thanks
You need to use the DateFormat Class
Something like this will get you the current time in the format you desire.
DateFormat.format("hh:mmaa dd/MM/yyyy", System.currentTimeMillis());
Use a SimpleDateFormat.
Format should be like
SimpleDateFormat sdf = new SimpleDateFormat( "HH:mma dd/MM/yyyy" );
sdf.format( yourDate );
Regards,
Stéphane
There are many ways to do that in Android. You can use the SimpleDateFormat wich is a class for formatting and parsing dates. Formatting turns a Date into a String, and parsing turns a String into a Date. Or you can the class Formatter wich is low level but managing the localization is your responsibility.
You may find source code example on the Android javadoc on those classes

Categories

Resources