Change Time zone and date format [duplicate] - java

This question already has answers here:
How to set the TimeZone for String parsing in Android
(3 answers)
Closed 6 years ago.
I get from the server is like 2017-01-24T16:16:30.690Z.
This date is in GMT time zone.
I want to convert this time into GMT+6 time zone as well as time format.
My expected result is: 24 January 2017 22:16

See above comments. If you apply those, try:
SimpleDateFormat sdf = new SimpleDateFormat();
sdf.applyPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = sdf.parse("2017-01-24T16:16:30.690Z", new ParsePosition(0));
sdf.setTimeZone(TimeZone.getTimeZone("GMT+06:00"));
sdf.applyPattern("d MMMM yyyy HH:mm");
String formatted = sdf.format(date);
Worked for me.

Related

SimpleDateFormat Japanese time [duplicate]

This question already has answers here:
Java Date() giving the wrong date [duplicate]
(9 answers)
Y returns 2012 while y returns 2011 in SimpleDateFormat
(5 answers)
Invalid date is populated when we use yyyy-MM-dd'T'HH:mm:ssXXX format in java [duplicate]
(1 answer)
Closed 2 years ago.
The time I have is -> September 15 2020 11:10:25
I am using this code to format it in Japanese
timeFormatStr = "YYYY MMMMMMMMMM DD HH:mm:ss z";
SimpleDateFormat sd = new SimpleDateFormat(timeFormatStr, locale);
timeStr = sdf.format(new Date(time));
The timeStr looks like this (does not look right).
2020 9月 259 23:10:25 UTC
Any idea what the format string should be? I checked that the locale is - ja_JP.eucjp
Thanks
YYYY MMMMMMMMMM DD HH:mm:ss z is not how the Japanese format their dates and times. You should use DateTimeFormatter, and call ofLocalizedDateTime and withLocale. This will produce a formatter that produces strings in a native Japanese format.
String formatted = DateTimeFormatter
.ofLocalizedDateTime(FormatStyle.FULL) // choose a style here
.withLocale(Locale.JAPANESE)
.format(new Date(time).toInstant().atZone(ZoneOffset.UTC)); // choose a timezone here
System.out.println(formatted); // 1970年1月1日木曜日 0時00分00秒 Z
You shouldn't really be using Dates anymore. You should instead give the DateTimeFormatter a ZonedDateTime directly.

Parsing timestamp with '+' into Date or Instant in Java [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 2 years ago.
I have timestamp of the format 2020-05-12 12:00:00+00 . How can I parse into Java.util.Date and Java.time.Instant.
Seemingly basic question, but I suspect it is the source of the problem that I am yet to solve in thread
Java 8 introduced DateTimeFormatter. Here is the link to the DateTimeFormatter Documentation.
For example:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ssx");
String tsFromDb = "2020-05-12 12:00:00+00";
Instant inst = formatter.parse(tsFromDb, Instant::from);
System.out.println(inst);
Output:
2020-05-12T12:00:00Z
If you need a java.util.Date:
Date oldfashionedDate = Date.from(inst);
System.out.println(oldfashionedDate);
Output in America/Tijuana time zone:
Tue May 12 05:00:00 PDT 2020

parse String to 'yyyy-mm-ddThh:mm:ss.SSSZ' ISODate java [duplicate]

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.

Creating date in java with SimpleDateFormat [duplicate]

This question already has answers here:
How to convert a String to a Date using SimpleDateFormat?
(10 answers)
Closed 7 years ago.
Update:
Really?!!! Duplicate??? My format is correct (yyyy/MM/dd HH:mm:ss) but return time is incorrect. How this is similar to another question????
I'm trying to create java Date but it's always return wrong value. This is my code:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:MM:SS");
Date GreDate = dateFormat.parse("2014/03/22 00:00:00");
And GreDate return Sun Dec 22 00:00:00 GMT+03:30 2013 as value.
Please don't suggest to use external library for date type.
Update:
I changed my pattern to this:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Now GreDate returns Sat Mar 22 01:00:00 GMT+04:30 2014. Year is correct but time still not 00:00:00.
Note that:
MM are the months, mm are the minutes.
SS are the milliseconds, ss are the seconds.
So you need to change your dateFormat to
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Basically there are two errors in your pattern, both in the time part (seconds and minutes).
Here is the link to the complete documentation link.

Can't parse using SimpleDateFormat [duplicate]

This question already has answers here:
Java - Unparseable date
(3 answers)
Closed 7 years ago.
I'm trying to parse "Dec 6 04:13:01" with "MMM d HH:mm:ss", but it is not working! I spent a lot of time but cant figure it out.
Any ideas why it fails?
You are probably trying to parse it with JAPANESE locale (guessing it from your profile + your web page), specify any english locale for example: Locale.US
String dateString = "Dec 6 04:13:01";
DateFormat df = new SimpleDateFormat("MMM d HH:mm:ss", Locale.US);
System.out.println(df.parse(dateString));

Categories