String dateTime = DateFormat.format("dd/MM/yyyy hh:mm aa", cal).toString();

80 Views Asked by At

How do I fix an error? When I send a message, the date appears correctly, but when I receive a message, the date appears incorrectly. For example, 5 p.m., the date appears at 8 p.m. Here is a three-hour difference.

AdapterChat:

Calendar cal = Calendar.getInstance(Locale.US);
cal.setTimeInMillis(Long.parseLong(timeStamp));
String dateTime = DateFormat.format("dd/MM/yyyy hh:mm aa", cal).toString();

ChatActivity:

String timestamp = String.valueOf(System.currentTimeMillis());

I tried changing Locale.US to Locale.getDefault, but the error was not fixed.

AdapterChat:

Calendar cal = Calendar.getInstance(Locale.getDefault());
cal.setTimeInMillis(Long.parseLong(timeStamp));
String dateTime = DateFormat.format("dd/MM/yyyy hh:mm aa", cal).toString();

ChatActivity:

String timestamp = String.valueOf(System.currentTimeMillis());
2

There are 2 best solutions below

0
WJS On

Don't use Calendar. Use classes from the java.time package.

As was stated, the sender/recipients can format a time stamp themselves in what ever form that meets their requirements. The following adjusts the time zone between East and West for a given Instant in time using your specified format.

Instant time = Instant.now();
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM/yyyy hh:mm a");
LocalDateTime ldt = LocalDateTime.ofInstant(time, ZoneId.of("US/Eastern"));

System.out.println(ldt.format(dtf));

ldt = LocalDateTime.ofInstant(time, ZoneId.of("US/Pacific"));
System.out.println(ldt.format(dtf));

prints

09/03/2024 02:24 PM
09/03/2024 11:24 AM
0
Basil Bourque On

Missing zone/offset

Your transmitted text lacks a vital part: Time zone or offset-from-UTC. You send/receive only a date & time-of-day. A date with time is inherently ambiguous. Does noon on January 23rd mean noon in Tokyo Japan, noon in Toulouse France, or noon in Toledo Ohio US — three different moments, several hours apart?

To determine a moment, a point on the timeline, you must provide all three elements:

  • date
  • time-of-day
  • time zone or offset

Avoid legacy date-time classes

Avoid the terribly flawed date-time classes such as Calendar and SimpleDateFormat. Those classes are now legacy, supplanted by the modern java.time classes defined in JSR 310.

ISO 8601

Do not exchange date-time values in localized formats. Use the standard ISO 8601 formats.

The java.time classes use ISO 8601 formats by default when parsing/generating text.

java.time.Instant

Capture the current moment as seen with an offset from UTC of zero hours-minutes-seconds by using Instant.

Instant instant = Instant.now() ;

Generate text in standard ISO 8691 format.

String output = instant.now() ;

2024-03-15T15:33:45.067355Z

The Z on the end is an abbreviation of +00:00, meaning an offset of zero. Pronounced “Zulu”.

The receiver can reconstitute an Instant object.

Instant instant = Instant.parse( input ) ;

java.time.ZonedDateTime

Adjust from UTC to a time zone to produce a ZonedDateTime object.

ZoneId z = ZoneId.of( "Pacific/Auckland" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;

Localized text

Generate text for presentation in localized format using DateTimeFormatter.

Locale locale = Locale.CANADA_FRENCH ;
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL ).withLocale( locale ) ;
String output = zdt.format( f ) ;

To learn more, search Stack Overflow. These topics have been addressed many times already.