This question already has answers here:
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
Closed 7 years ago.
I could not find a correctly and clean working solution for my Date which is formatted like this:
2014-06-09T00:01+0200
(9th of June here)
Last I tried was this:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmXXX", Locale.ENGLISH);
This just gives me an unparsable date exception. What do you think I should change?
replace XXX with Z,
String dateTimestr = "2014-06-09T00:01+0200";
SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmZ");
s.parse(dateTimestr);
to print,
System.out.println(s.format(s.parse(dateTimestr)));
using Java 8,
OffsetDateTime dateTime = OffsetDateTime.parse("2014-06-09T00:01+02:00");
System.out.println(dateTime.toString());
Note that OffsetDateTime.parse would only work if your string is proper ISO8601 date format. If your date is in different format then you have to provide your OffsetDateTime.parse() with a proper formatter using DateTimeFormatter. i.e
OffsetDateTime.parse(yourStringDate, DateTimeFormatter formatter)
Use Z instead of XXX or one X
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmZ", Locale.ENGLISH);
From the documentation:
Z Time zone RFC 822 time zone -0800
X Time zone ISO 8601 time zone -08; -0800; -08:00
The problem is XXX requires timezone format with a colon, i.e., 2014-06-09T00:01+02:00
Using Z instead of XXX or using XX (2 X's) should accept format without colon
Be careful with using one X as some have posted because this will disregard the last two digits of the timezone (e.g., 2014-06-09T00:01+02). This could be problematic if working with time zones in some countries like India where the time zone offset is +05:30. Note the following code..
System.out.println(new SimpleDateFormat("yyyy-MM-dd'T'HH:mmXXX").format(new SimpleDateFormat("yyyy-MM-dd'T'HH:mmX").parse("2014-06-09T00:01+05:30")));
Will print 2014-06-08T14:01-05:00. The :30 in the timezone was lost when using one X. Documentation
Related
This question already has answers here:
Convert Java Date to UTC String
(7 answers)
How do you format current system datetime as UTC using String.format in Java?
(2 answers)
How to get UTC+0 date in Java 8?
(5 answers)
Closed last month.
This post was edited and submitted for review last month and failed to reopen the post:
Original close reason(s) were not resolved
I am trying to print a date in this format:
2023-01-11 09:25:52 UTC
But when I use date format:
yyyy-MM-dd HH:mm:ss Z
I get:
2023-01-11 09:29:25 +0100
While searching by existing question in stack overflow, I found similar questions but not with this exact format with "UTC" at the end. I found one that provided a solution to add the format as yyyy-MM-dd HH:mm:ss 'UTC' but then it would force the a GMT value to wrongly show UTC at the end.
Most of the answers explain how to get UTC value, but not how to print in this format
2023-01-11 09:25:52 UTC
Some solutions were also suggesting to use something else than SimpleDateFormat.
This question was marked as duplicated, but none of the post that were supposed to be duplicated had the info that I wanted.
Use a lowercase z instead of Z to get the offset instead of the id. And you have to set the time zone using simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")).
Example:
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(simpleDateFormat.format(date)); // 2023-01-11 08:41:17 UTC
If you create an instance of simpleDateFormat with yyyy-MM-dd HH:mm:ss Z date format is incorrect change to capital Z to small z,
then it works as expected.
2023-01-11 09:25:52 UTC
This question already has answers here:
Unix epoch time to Java Date object
(7 answers)
Closed 5 years ago.
How can I convert minutes from Unix timestamp to date and time in java? For example, timestamp 1372339860 correspond to Thu, 27 Jun 2013 13:31:00 GMT.
I want to convert 1372339860 to 2013-06-27 13:31:00 GMT.
Edit: Actually I want it to be according to US timing GMT-4, so it will be 2013-06-27 09:31:00.
You can use SimlpeDateFormat to format your date like this:
long unixSeconds = 1372339860;
// convert seconds to milliseconds
Date date = new java.util.Date(unixSeconds*1000L);
// the format of your date
SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
// give a timezone reference for formatting (see comment at the bottom)
sdf.setTimeZone(java.util.TimeZone.getTimeZone("GMT-4"));
String formattedDate = sdf.format(date);
System.out.println(formattedDate);
The pattern that SimpleDateFormat takes if very flexible, you can check in the javadocs all the variations you can use to produce different formatting based on the patterns you write given a specific Date. http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Because a Date provides a getTime() method that returns the milliseconds since EPOC, it is required that you give to SimpleDateFormat a timezone to format the date properly acording to your timezone, otherwise it will use the default timezone of the JVM (which if well configured will anyways be right)
Java 8 introduces the Instant.ofEpochSecond utility method for creating an Instant from a Unix timestamp, this can then be converted into a ZonedDateTime and finally formatted, e.g.:
final DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
final long unixTime = 1372339860;
final String formattedDtm = Instant.ofEpochSecond(unixTime)
.atZone(ZoneId.of("GMT-4"))
.format(formatter);
System.out.println(formattedDtm); // => '2013-06-27 09:31:00'
I thought this might be useful for people who are using Java 8.
You need to convert it to milliseconds by multiplying the timestamp by 1000:
java.util.Date dateTime=new java.util.Date((long)timeStamp*1000);
This question already has answers here:
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
Closed 5 years ago.
I have a DateFormat like
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
I have a date like this:
2017-02-23T11:00:04.072625
This "date" is a UTC date (Z is null). This parses fine. However, it seems to be interpreted as the timezone of the machine. So, my machine is EST, this ends up as
2017-02-23T11:00:04.072625-0500
Which is wrong. I can explicitly set the timezone with like
format.setTimeZone(TimeZone.getTimeZone("UTC"));
But if the time came in a different zone in the future, that would not be right either.
If I add a "Z" or "z" to the SimpleDateFormat string, this fails to parse.
Any ideas on how to handle this correctly?
First, since you are using Java 8, start by discarding the old classes Date, DateFormat, SimpleDateFormat and TimeZone. They will more likely than not give you trouble you don’t want. Instead use the classes in java.time introduces in Java 8. Here you go:
String yourDateTime = "2017-02-23T11:00:04.072625";
LocalDateTime ldt = LocalDateTime.parse(yourDateTime);
Instant i = ldt.atOffset(ZoneOffset.UTC).toInstant();
Instant is the new class that best corresponds to the old Date. In this case you get
2017-02-23T11:00:04.072625Z
(Z means UTC)
If in the future you get the date in a different time zone, you may do something like
ZoneId zi = ZoneId.of("Europe/Paris");
Instant i = ldt.atZone(zi).toInstant();
In this case you get instead
2017-02-23T10:00:04.072625Z
Should you for some reason require an oldfashioned Date instance, for example for use with old code, it’s easy:
Date oldfashionedDate = Date.from(i);
I have a String, 2013-10-07T23:59:51.205-07:00, want to convert this to Java date object. I am getting parsing error.
date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").parse("2013-10-07T23:59:51.205-07:00");
try
date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
.parse("2013-10-07T23:59:51.205-0700");
The Z is not a literal and the timezone does not have a colon
See the examples at http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
If java7 is being used then Z can be replaced with X and the timezone can have a colon
Z shouldn't be inside quotes. I don't think Z would work for your given timezone. Before Java 7, I guess there wasn't any format to parse ISO 8601 format timezone with colon in between. You should use -0700 instead.
However, from Java 7 onwards, you have an option for parsing ISO 8601 format timezone using X instead of Z. See javadoc for SimpleDateFormat. Just use the following format:
// This would work from Java 7 onwards
date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX")
.parse("2013-10-07T23:59:51.205-07:00");
Your pattern is wrong, you should use the following:
date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
.parse("2013-10-07T23:59:51.205-07:00");
The 'X' indicates the Time zone in the ISO 8601 format as expressed in your String here: '.205-07:00'
For more information read the doc: SimpleDateFormat
Use this trick to parse ISO8601 datetime format. I admit have not tried this with millisecond part within a string value maybe it gives you an extra headache. This works for Java6.
import javax.xml.bind.DatatypeConverter;
Calendar cal = DatatypeConverter.parseDateTime(strDatetime);
If am remembering correct cal instance may not use a system-default timezone. Its initialized to the origin string value timezone. If you want instance to use system timezone you can do this conversion.
long ts = cal.getTimeInMillis();
cal = Calendar.getInstance();
cal.setTimeInMillis(ts);
You should use XXX for the format -07:00, instead of Z and X.
Date sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
.parse("2013-10-07T23:59:51.205-07:00");
Look at the example of this docs.
The problem is that -07:00 is not a valid Time zone . The Time Zone should have this format, for example something like -0800.
This question already has answers here:
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
Closed 6 years ago.
The time that I am getting is in the format "2011-07-31T08:16:37.733Z". Actually the Z should be the timezone and then that time is converted to local timezone. How do I actually convert this time to default timezone now.
RFC 3339 describes a specific ISO 8601 'profile' for date/time representation.
If using 3rd-party open-source libraries is an option, take a look at Joda-Time's ISODateTimeFormat utility class.
Otherwise, if you need to roll out your own code it's not enough to use a SimpleDateFormat because of the way ISO 8601/RFC 3339 represents timezone information.
If all you need to parse is in 'Z' (Zulu or UTC), then it's relatively simple:
String input = "2011-08-11T01:23:45.678Z";
TimeZone utc = TimeZone.getTimeZone("UTC");
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
f.setTimeZone(utc);
GregorianCalendar cal = new GregorianCalendar(utc);
cal.setTime(f.parse(input));
System.out.println(cal.getTime());
Should print out "2011-08-11T01:23:45.678Z" in your local time zone.
If you need to handle arbitrary timezones in RFC 3339 then you'll need to extend the above to parse out the timezone designator and retrieve the corresponding Java TimeZone instance. That shouldn't be too hard if it's just whole hours +/- GMT, but in case of non-standard offsets you may have to create your own SimpleTimeZone instance.
private static SimpleDateFormat DATE_TIME_FORMAT=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
DATE_TIME_FORMAT.setTimeZone(TimeZone.getID()); //for example
date=DATE_TIME_FORMAT.format(curDate);