I have get web service response in my android app in json format. In this response I need to find a date from json. I am getting date "/Date(1381363200000)/" in this format, now to have to convert it to JAVA date object and then get a simple date like : "20-june-2013". I have done some r&d in Google I found about Gson but I be more confused. Please any body tell me how can I achieve this.
Thank you in advance...
The date is in epoch format. All you need to do, is convert it from epoch into a human readable date.
Here's an example of how to do it in Java
Date date = new Date(1381363200000);
DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String formatted = format.format(date);
System.out.println(formatted);
HTH
Related
I have json string which is returned from mongodb.
[{"_id":"578bb51aa51d15940688809e","name":"aaa","date":"2016-07-20T11:47:39.302Z"}]
I used gson to convert the Java Object. I got Unparseable date: "2016-07-20T11:47:39.302Z". So I added date format to gson builder.
Gson gson= new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").create();
The date result is Wed Jul 20 11:47:39 MMT 2016. The time is incorrect.
What is the better date format pattern for date string?
How can I choose the suitable date format pattern by the given date string?
If I am not mistaken, you are having an issue with timezone information. Both the JavaScript Date object and the Java Date object represent a point in universal coordinated time (UTC). Neither the JavaScript Date object nor the Java Date object contain timezone information.
Side note: The Java Date object does actually contain timezone information, which is why you see the timezone (MMT in your case) when you call date.toString(). Though, this timezone should be ignored, as the timezone irregularities highlight some of the many issues with the Java 7- date/time classes.
The GsonBuilder().setDateFormat() method sets the string format that a Java Date object should be serialized to and deserialized from. This format does not contain any timezone information either because the Date object does not contain that information.
In order to display the UTC time your Date object represents in the correct timezone, you must provide that timezone. This is not done during deserialization with Gson, but when you actually display the date/time:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getDefault());
String zonedDateTime = simpleDateFormat.format(date);
Android also comes with a handy DateUtils class to use instead of SimpleDateFormat that will handle the timezone information for you, as well as Locale information.
DateUtils.formatDateTime(getContext(), date.getTime(),
DateUtils.FORMAT_NUMERIC_DATE | DateUtils.FORMAT_ABBREV_ALL);
Hi in a JSON response i have the two dates, "2016-03-29T00:00:00+01:00" and "2016-03-27T00:00:00Z" in this structure something like [{date:2016-03-29T00:00:00+01:00},{date:2016-03-27T00:00:00Z}] how to parse this using Gson or GsonBuilder. Please keep in mind using android.
Thanks
these are iso8601 compliant time stamps there is 660kb library for parsing strings like these called joda time gson is a json parser not a timestamp parser, you can take a look at this answer for converting iso8601 time stamps to java.util.date , both the question and the answer explain in detail how to do this.
If the format of the date string isn't going to change then for now you could try this.
SimpleDateFormat ISO8601DATEFORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);
Date date = ISO8601DATEFORMAT.parse(date);
I personally had been using the format yyyy-MM-dd'T'HH:mm:ss.SSSZ for quite a while to parse iso8601 timestamps, but have now moved to using joda time.
There is setDateFormat() method in GsonBuilder where you can specify date format you need
How do you convert a date to the date format of the device? For example, I have following date: 02/11/2011 (mm/dd/yyyy). How to convert it to the format of the device?
DateFormat has the static methods getDateTimeInstance, getTimeInstance and getDateInstance that are already localized
You might want to consider storing your dates in a more locale-agnostic way as milliseconds.
Then you can use DateFormat.getDateFormat() like this:
dateTextView.setText(DateFormat.getDateFormat(getActivity()).format(new Date(millis)));
According to the documentation for getDateFormat():
Returns a DateFormat object that can format the date in short form (such as 12/31/1999) according to the current locale and the user's date-order preference.
Use SimpleDateFormat to convert a Date into required format.
See this link for more details..
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
String formattedDate = format.format(yourdate);
I'm retrieving hours data for places from a service (Factual). It comes to me in 24-hour format and I need to display it in 12-hour format. The data for a specific day comes like this:
"sunday\":[[\"12:00\",\"21:30\"]]
I can successfully retrieve the hours from the JSON. Then, using SimpleDateFormat, I can parse the string to a Date object. But, then I can't figure out how to convert them to 12;-hour format so that I can display them as "12:00 - 9:30" or "12:00pm - 9:30pm" rather than "12:00 - 21:30".
How can I go about doing this? Thanks!
EDIT:
By parsing the string of hours (i.e. "12:00") using SimpleDateFormat sdf2 = new SimpleDateFormat("hh:mm a");, I get an error from JSON saying that the value is unparseable. If I use just SimpleDateFormat sdf2 = new SimpleDateFormat("hh:mm");, then there's no error but I can't get things to show up in 12-hour format.
If you look in the simple date format syntax docs you will find that 'h' is used for 12-hour time and 'a' is used for AM/PM. You will need to extract the two times using substring before putting them through the dateformatters.
http://developer.android.com/reference/java/text/SimpleDateFormat.html
SimpleDateFormat in = new SimpleDateFormat("<input format goes here>");
Date d = in.parse(INPUT_DATE_STRING);
SimpleDateFormat out = new SimpleDateFormat("<output format goes here>");
String outDate = out.format(d);
Try this:
//Char sequence for a 12 hour format.
CharSequence DEFAULT_FORMAT_12_HOUR = "hh:mm a";
//Char sequence for a 24 hour format.
CharSequence DEFAULT_FORMAT_24_HOUR = "kk:mm";
//date is the Date object. Look for more functions in format.
DateFormat.format(DEFAULT_FORMAT_12_HOUR, date);
Let me know if it works. If you have any issue check the Date you are sending.
//This should give you the default time on the device. To show that it works.
DateFormat.format(DEFAULT_FORMAT_12_HOUR, Calendar.getInstance());
I've got following string "2013-04-30T00:55:25.855-07:00" from Google blogger feed. I try to save this string to SQLite datetime field as following command
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
initialValues.put(newsdatemodified, dateFormat.format(datemodified));
But I found that data cannot insert into sqlite table and no encounter any errors. Any solution will be appreciated.
Update
When I've tried to use following coding
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date date1 = (Date) dateFormat.parse(entry.updated);
but following this error
05-02 09:56:05.383: E/AndroidRuntime(31634): Caused by: java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date
Your format is wrong, it needs to me yyyy-MM-dd'T'HH:mm:ss.SSSZ
as you can see the format you had does not match what the incoming string format was
It seems that issue is with the format. May be, you can try using yyyy-MM-dd'T'HH:mm:ss.SSSZ. SSS stands for fractional seconds and Z stands for time zone.
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Hope it helps else please comment. You can see other formats and try to work around with them by seeing here.