SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); customize the timezone

447 Views Asked by At

I want to print the date with the char ":" in the middle of timezone, example :

2023-08-02T14:19:10+02:00

At the moment I am using this pattern:

SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");

and it prints:

2023-08-02T14:19:10+0200

Any idea?

below an example of code :

public class testDate {

    public static final DateFormat PATTERN = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");

    public static String calendarToString(final Calendar dateToConvert, final DateFormat df) {
        return df.format(dateToConvert.getTime());
    }

    public static void main(String[] args) {
        Calendar effectiveDate = Calendar.getInstance();
        System.out.println(calendarToString(effectiveDate, PATTERN));
    }
}
3

There are 3 best solutions below

0
Mark Rotteveel On BEST ANSWER

You need to use XXX instead of Z to get a timezone formatted as {Z|{+|-}HH:mm}. That said, you should really switch to using the new java.time types, and stop using SimpleDateFormat.

This is shown in one of the examples in the SimpleDateFormat documentation:

Date and Time Pattern Result
... ...
"yyyy-MM-dd'T'HH:mm:ss.SSSXXX" 2001-07-04T12:08:56.235-07:00
0
Mr. Polywhirl On

If you are working with Java 1.8+, you should use classes from the java.time package.

The format you use for the java.text.SimpleDateFormat class should also work with the java.time.format.DateTimeFormatter class.

Here is an example:

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class ZonedDateTimeFormatExample {
  public static void main(String... args) {
    ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Africa/Cairo"));
    DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
    System.out.printf("Current date and time in Cairo, Egypt: %s%n", fmt.format(now));
  }
}

Avoid using the java.text and java.util packages for date/time/calendar parsing and formatting.

This includes, but is not limited to:

  • java.util.Calendar
  • java.util.Date
  • java.text.SimpleDateFormat

Please review: Java SE 8 Date and Time

3
Basil Bourque On

tl;dr

  • You are working too hard.
  • Use modern java.time. Never use Calendar & SimpleDateFormat.
  • No need to specify a formatting pattern for standard format.
  • Truncate your unwanted fractional second.
OffsetDateTime.now().truncatedTo( SECONDS ).toString()

2023-08-02T14:19:10+02:00

java.time.OffsetDateTime

You are using terrible date-time classes that were years ago supplanted by the modern java.time classes defined in JSR 310.

For a date with time-of-day in the context of an offset-from-UTC, use the class OffsetDateTime.

First specify your desired offset.

ZoneOffset offset = ZoneOffset.ofHours( 2 ) ;  // +02:00

Capture the current moment as seen in that offset.

OffsetDateTime odt = OffsetDateTime.now( offset ) ;

Since you have no interest in the fractional second, truncate.

OffsetDateTime odt = odt.truncatedTo( ChronoUnit.SECONDS ) ;

Generate text in your desired format which happens to comply with the ISO 8601 standard formats used by default in java.time when generating/parsing text. So no need to specify a formatting pattern.

String output = odt.toString() ;

2023-08-02T14:19:10+02:00

Rather than hard-code an offset, you may want to specify a time zone to automatically determine the offset in effect at that point in time.

String output = 
    ZonedDateTime
    .now(
        ZoneId.of( "Africa/Gaborone" ) 
    )
    .toOffsetDateTime()
    .truncatedTo( ChronoUnit.SECONDS )
    .toString()
;