I am working with JodaTime now and have a question about how to parse String into DateTime.
I have got a date String in the format:
"2013-05-14T11:36:08+0000"
I tried to convert it into JodaTime's DateTime object:
DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss");
DateTime dateTime = fmt.parseDateTime("2013-05-14T11:36:08+0000");
It works fine except that if I call
dateTime.getHourOfDay()
It returns me 12 instead of 11.
Surprisingly, if I use the Java's SimpleDateFormat:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date date = df.parse("2013-05-14T11:36:08+0000");
The date contains exactly the same result, the hour is 12 instead of 11.
I am based in London. I started to think whether this is because the summer saving time? Or did I make an mistake on how the String should be parsed?
Please help. Many thanks.
Related
I am receiving a timestamp like : 07/23/2019.08.45 from a system.
I need to convert this to Epoch.
I was not able to parse this format by using SimpleDateFormat.
I tried:
SimpleDateFormat SDF = new SimpleDateFormat("MM/dd/yyyy.HH:mm");
Date dateInstance =SDF.parse("07/23/2019.08.45");
but it didn't work
I also tried to split and parse the time and date but still got the unable to parse error
Can someone please help.
What would be the most efficient way to get this done.
You are not providing the correct date format:
SimpleDateFormat SDF = new SimpleDateFormat("MM/dd/yyyy.HH:mm");
Date dateInstance =SDF.parse("07/23/2019.08:45");
Notice : instead of .
This question already has answers here:
Java / convert ISO-8601 (2010-12-16T13:33:50.513852Z) to Date object
(4 answers)
Closed 6 years ago.
How to convert String to ISODate format using SimpleDateFormat, which means that i want to finally get Date in java.util.Date format.
My string will look like 2017-02-17T09:28:03.000Z, i want to convert it to date formt. I can do this in Joda date format, but since i am using mongoDB, it does not accept joda format.
String startDateString1 = "2017-02-17T04:23:17.452Z";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date startDate = df.parse(startDateString1);
String newDateString = df.format(startDate);
above code is not working.
Your code is not working since Z is a reserved character used to interpret RFC-822 time zones :
RFC 822 time zone: For formatting, the RFC 822 4-digit time zone format is used:
RFC822TimeZone:
Sign TwoDigitHours Minutes
TwoDigitHours:
Digit Digit
Since Java 7, you can use X to interpret ISO-8601 time zones https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html . The following works :
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
However, on my computer,
System.out.println(newDateString);
results in the following output :
2017-02-17T05:23:17.452+01
Alternatively, if you are sure to have only UTC dates, you could escape the Z letter with simple quotes :
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
And here is the displayed result :
2017-02-17T04:23:17.452Z
You can do it in Java 8 like below.
Instant instant = Instant.parse("2017-02-17T09:28:03.000Z");
Date date = Date.from(instant);
You could use javax.xml.bind.DatatypeConverter.parseDateTime("2017-02-17T04:23:17.452Z") which will return a Calendar object. You can call getTime() on it to get a Date object.
I'm trying to upload the current UTC time to a date "field" in Parse. How can I do this?
DateFormat df = DateFormat.getTimeInstance();
df.setTimeZone(TimeZone.getTimeZone("utc"));
String utcTime = df.format(new Date());
message.put("lastReplyUpdatedAt", utcTime);
message.saveInBackground();
Unfortunately, I can't seem to get the right imports to work for the classes above so I can't construct my String. What can I do here? Also I'm not sure if Parse would even accept that String. Are there any special formats I need to consider?
Pass a Date
From the Parse platform documentation, pass a Date object rather than a String.
Date myDate = new Date();
message.put("lastReplyUpdatedAt", myDate);
message.saveInBackground();
I have read a bunch of posts on this, but, I am obviously missing something. I have date string, and a time zone. I am trying to instantiate a date object as follows:
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
java.util.Date dateObj = sdf.parse("2013-10-06 13:30:00");
System.out.println(dateObj);
What is printed is:
Sun Oct 06 09:30:00 EDT 2013
What I want is a date object in UTC format. Not one converted to EDT. What am I doing wrong?
Thanks.
This is because a Date object does not store any timezone information. Date basically only stores the number of milliseconds since the epoch (Jan. 1, 1970). By default Date will use the timezone associated with the JVM. In order to preserve timezone information you should continue using the DateFormat object that you've already got.
See DateFormat#format(Date): http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html#format(java.util.Date)
The following should give you what you're looking for:
System.out.println(sdf.format(dateObj));
Try below code, you'll see that the date parsed 1st time is different from the one parsed after setting timezone. Actually the date is parsed as expected in right timezone. It s while printing it gives you get the machines's default TZ.
You could have printed the dateObj.toGMTString() to check the same, but that is deprecated.
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dateObj = sdf.parse("2013-10-06 13:30:00");
System.out.println(dateObj.toString());
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
dateObj = sdf.parse("2013-10-06 13:30:00");
System.out.println(dateObj.toString());
Okay, so here's my issue in Android right now. On our Database there's a timestamp in this format 8/15/2013 2:00:48 PM and through a .NET WebService I get that same time like this in Android: 2013-08-15T14:00:48-07:00. Now I want to convert this format into a Date Time format that I can use for comparison (for example this webservice provides every instance where a device failed at logging in so we want to check the amount of time between occurances to see if there's any issues). Below I have this code where I'm trying to use JODA Time but it's still not returning the correct format:
public static Date convertStringToDate(String input) {
String pattern = "yyyy-MM-dd'T'HH:mm:ssZ";
DateTimeFormatter formatter = DateTimeFormat.forPattern(pattern);
DateTime dateTime = formatter.parseDateTime(input);
return dateTime.toDate();
//printout shows: Thu Aug 15 17:00:48 EDT 2013
}
I know that the server is returning some crappy time format that is hard to work with (it took a while to get this to work in the iOS App we have, and even there it's still rather clunky) so I don't mind changing the webservice or the query if that would make things easier.
I have a very similar format, and I parse it using SimpleDateFormat, try this:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZ", Locale.US);
Date dateTime = format .parse(value);
What i understand is that you have your correct instance of date already and what you need is to parse it to String.
I suggest you use:
DateFormat formatter = new SimpleDateFormat("d/MM/yyyy hh:mm:ss a");
//this will give you the format '8/15/2013 2:00:48 PM'
String d = formatter.format(date);
Hope this helps.
EDIT:
Also seams you want to have your date instance in -07:00 timezone
So you can change your line
DateTime dateTime = formatter.parseDateTime(input);
for
DateTime dateTime = formatter.withZone(DateTimeZone.forID("-07:00")).parseDateTime(input);