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.?
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.?
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]
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));
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()));
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()));