I have this from my server:
2017-04-07T11:00:00.000Z
How can I convert it to local time? Here is my code, but all I got is an exception:)
java.lang.IllegalArgumentException: Invalid format: "2017-04-07T11:00:00.000Z" is malformed at "Z"
DateTimeFormatter inputFormatter = DateTimeFormat
.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ") //yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ
.withLocale(Locale.US);
DateTime parsed = inputFormatter.parseDateTime(gmtDate);
DateTimeFormatter outputFormatter = DateTimeFormat
.forPattern("HH:mm")
.withLocale(Locale.US)
.withZone(DateTimeZone.getDefault());
return outputFormatter.print(parsed);
Just change your pattern to yyyy-MM-dd'T'HH:mm:ss.SSSZ, like this:
DateTimeFormatter inputFormatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
.withLocale(Locale.US);
In my case, the result of outputFormatter.print(parsed) is 08:00 (my local time, as my default timezone is America/Sao_Paulo)
Related
I want to parse the following String :
2022-02-14T04:40:33.000000Z
So I'll be able to convert it to this date format:
14/Feb/2022 04:40 or dd/MMM/yyyy HH:mm.
But it throws an exception :
Caused by: java.text.ParseException: Unparseable date: "2022-02-14T04:40:33.000000Z"
My code is a basic SimpleDateFormat with the following code :
val datetime = "2022-02-14T04:40:33.000000Z"
val inputFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.getDefault())
val outputFormat = SimpleDateFormat("dd/MMM/yyyy HH:mm", Locale.getDefault())
val inputDate = inputFormat.parse(date) // exception thrown here.
val dateString = outputFormat.format(inputDate)
I had tried the following format :
yyyy-MM-dd'T'HH:mm:ss.SSSZ
yyyy-MM-dd'T'HH:mm:ss.SSS'000'Z (I was expecting to ignore the extra 000)
I had read about the PHP DateTime::format which had the u format character for microseconds.
But, the android SimpleDateFormat had no support for the microseconds character.
Please use following format
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
I have the following String:
18/07/2019 16:20
I try to convert this string into LocalDateTime with the following code:
val stringDate = expiration_button.text.toString()
val date = LocalDateTime.parse(stringDate, DateTimeFormatter.ofPattern("dd/MM/yyyy hh:mm")).toString()
java.time.format.DateTimeParseException: Text '18/07/2019 04:30:00'
could not be parsed: Unable to obtain LocalDateTime from
TemporalAccessor
What I'm missing?
I think this will answer your question:
val stringDate = expiration_button.text.toString()
val formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy hh:mm");
val dt = LocalDate.parse(stringDate, formatter);
Edit 1:
It's probably crashing because you are using a 12hr Hour, instead of a 24hr pattern.
Changing the hour to 24hr pattern by using a capital H should fix it:
val dateTime = LocalDateTime.parse(stringDate, DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm"));
Use below to convert the time from String to LocalDateTime, but make sure you are getting the time in String form.
String str = "2016-03-04 11:30";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
Btw, If your String contains seconds as well like "2016-03-04 11:30: 40", then you can change your date time format to yyyy-MM-dd HH:mm:ss" as shown below:
String str = "2016-03-04 11:30: 40";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
Change your datetime format to "dd/MM/yyyy hh:mm a" and provide a string date with additional AM/PM information, e.g. val stringDate = "18/07/2019 04:20 PM" or just use the 24 hour format "dd/MM/yyyy HH:mm".
You may try using "-" instead of "/" on the date.
I added this jar file joda-time-2.0.jar in my android project . After adding this jar file it cannot taken the .ofpattern() , please solve this one. thank you.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
Using Joda time you would do it like this:
String dateTime = "11/15/2013 08:00:00";
// Format for input
DateTimeFormatter dtf = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss");
// Parsing the date
DateTime jodatime = dtf.parseDateTime(dateTime);
// Format for output
DateTimeFormatter dtfOut = DateTimeFormat.forPattern("MM/dd/yyyy");
// Printing the date
System.out.println(dtfOut.print(jodatime));
I tried to parse this date 2017-03-04**T**17:20:55Z to Unix timestamp and I received a parse error for the T char (bolded it).
I tried to use new SimpleDateFormat and DateTimeFormatter. How can I get a Unix timestamp from this DateTime format?
You can use single quotes to escape characters in the format string of SimpleDateFormat:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Date date = dateFormat.parse("2017-03-04T17:20:55Z");
In my application the Date Fiend JSON response comes as this type:
"CreatedOn": "\/Date(1313572467987+0000)\/"
I want to convert this date in the "MM DD YYYY" format. How can I convert this?
That date in your JSON response looks like a standard timestamp (e.g. number of milliseconds since January 1, 1970). If you parse out the timestamp something like:
String timestamp = jsonValue.split("\\(")[1].split("\\+")[0];
Date createdOn = new Date(Long.parseLong(timestamp));
Now you can use SimpleDateFormat to format a date string:
SimpleDateFormat sdf = new SimpleDateFormat("MM dd yyyy");
String formattedDate = sdf.format(createdOn);
This is ignoring the timezone adjustment in that response (the '+0000') you might also want to parse out this value and add/remove the hours from your timestamp value before formatting..
you can use SimpleDateFormat, so just try to search on the same.
Detailed example is here: http://www.helloandroid.com/tutorials/date-handling-android-development