-1
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
f.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println( f.format(new Date()));

This will be in UTC timezone , i need to set in UK(European/london) timezone.?

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • This answer to a previous question might help - https://stackoverflow.com/a/2540295/15310387 – Mr R May 04 '21 at 06:15

4 Answers4

0

Stop using the legacy java.util.Date class and use ZonedDateTime instead.

    ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Europe/London"));
    System.out.println(now.format(DateTimeFormatter.ISO_DATE_TIME));

Outputs: 2021-05-04T07:15:22.2556157+01:00[Europe/London]

maio290
  • 6,440
  • 1
  • 21
  • 38
0
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");

OffsetDateTime dateTime = OffsetDateTime.parse("2020-01-01T00:00:00Z");
OffsetDateTime dateTimeLondon = dateTime.atZoneSameInstant(ZoneId.of("Europe/London")).toOffsetDateTime();

System.out.println(formatter.format(dateTimeLondon));
Thomas Preißler
  • 613
  • 4
  • 13
0

I guess you need London timezone, it would be replace UTC with time zone you need in the getTimeZOne method.

SimpleDateFormat f = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");
f.setTimeZone(TimeZone.getTimeZone("Europe/London"));
System.out.println(f.format(GregorianCalendar.getInstance().getTime()));
Kathikeyan
  • 71
  • 6
0

There is various way to do it. but in your question, You want specifically for European/London. TimeZone will give you a facility to do this.

SimpleDateFormat sdF= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
sdF.setTimeZone(TimeZone.getTimeZone("Europe/London"));
System.out.println(sdF.format(new Date()));
Negi Rox
  • 3,828
  • 1
  • 11
  • 18