0

If i have:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("Europe/London"));

String dateStr = dateFormat.format(new Date());
System.out.println(dateStr);

The output will be: 2014-09-30 14:19:17 when my local time is 2014-09-30 16:19:17

I need to get Date type in specific timezone and not String Type!

My question is , how can i get new Date() on "Europe/London" timezone,

Yosefarr
  • 709
  • 5
  • 12
  • 26

2 Answers2

1

You asked:

My question is, how can i get new Date() on "Europe/London" timezone

You can't. The java.util.Date class represents an instant in time. It is not aligned to any local time zone.

Internally, it tracks the number of milliseconds since Midnight, January 1st, 1970 UTC. A local time zone might be applied when you use it, but there is no time zone setting contained within the Date class itself.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
0

Use Calendar , for example :

TimeZone timeZone= TimeZone.getTimeZone("Europe/London"));

Calendar cal = Calendar.getInstance(timeZone);

also , the good thing about dates is that they arent dependant of TimeZone , its just time in millis since epoch (some time a while back) . The way you are using it now is actually the right solution .

vlatkozelka
  • 909
  • 1
  • 12
  • 27