Initially, the SpringFramework WAR application was deployed in a Server in which date format in JSON Result Oct 27, 2021 11:23:48 AM
even in my local the same.
But the same application is deployed into a new server, now the date format in the result is quite different Oct 27, 2021, 3:38:02 PM
Extra comma has been added after the year.
the old server is EST timezone, now the new server is UTC timezone. Without changing time zone What should I do at the code level?
What is the issue? how can I have the same date format as in Old Server?
CodePudding user response:
Try with ZonedDateTime.
LocalDateTime localDateTime = LocalDateTime.now();
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.of("UTC"));
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd, yyyy HH:mm:ss");
String formattedString = zonedDateTime.format(formatter);
CodePudding user response:
What is the issue?
A java.util.Date
object simply represents an instant on the timeline — a wrapper around the number of milliseconds since the UNIX epoch (January 1, 1970, 00:00:00 GMT). Since it does not hold any Timezone
and Locale
information, its toString
function applies the JVM's Timezone
and Locale
to return a String
in the format, EEE MMM dd HH:mm:ss zzz yyyy
, derived from this milliseconds value. To get the String
representation of the java.util.Date
object in a different format, Timezone
and Locale
, you need to use SimpleDateFormat
with the desired format and the applicable Timezone
and Locale
e.g.
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.ENGLISH);
sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));
String strDateNewYork = sdf.format(date);
System.out.println(strDateNewYork);
sdf.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
String strDateUtc = sdf.format(date);
System.out.println(strDateUtc);
So you see different representations of the same instant i.e.
Oct 27, 2021 11:23:48 AM at America/New_York = Oct 27, 2021, 3:38:02 PM at UTC
because your server is applying the server's timezone by default. If you want the values to be always in a fixed Timezone
, specify the same explicitly as shown above.
how can I have the same date format as in Old Server?
If you want the values to be always in a fixed Locale
, specify the same explicitly as shown above.
Switch to java.time
:
The java.util
Date-Time API and their formatting API, SimpleDateFormat
are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.
You can use java.time.Instant
whose toString
method uses ISO-8601 representation and therefore the output remains the same irrespective of the timezone setting of the server.
Demo:
import java.time.Instant;
public class Main {
public static void main(String[] args) {
Instant now = Instant.now();
System.out.println(now);
}
}
If you want to display this instant in different timezones, you can obtain the respective ZonedDateTime
by using Instant.atZone
.
Demo:
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class Main {
public static void main(String[] args) {
Instant now = Instant.now();
System.out.println(now);
ZonedDateTime zdtNewYork = now.atZone(ZoneId.of("America/New_York"));
System.out.println(zdtNewYork);
ZonedDateTime zdtIndia = now.atZone(ZoneId.of("Asia/Kolkata"));
System.out.println(zdtIndia);
}
}
Learn more about the modern Date-Time API from Trail: Date Time. Check this answer and this answer to learn how to use java.time
API with JDBC.
* If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8 APIs available through desugaring. Note that Android 8.0 Oreo already provides support for java.time
.