4
    Date now = new Date();
    Date then = new Date((long)obj.timestamp*1000);

    TimeZone tz = TimeZone.getDefault();

Not very familiar with java, but is there any way to apply a timezone to a Date object? I found this thread, but this is about Calendar timezones, which i believe is something different?

Community
  • 1
  • 1
Johan
  • 35,120
  • 54
  • 178
  • 293

3 Answers3

3

Date object uses the current timezone by default. If you are trying to print the time in specific timezone, you may use SimpleDateFormat as below:

   //put the desired format      
   DateFormat formatter= new SimpleDateFormat("MM/dd/yyyy hh:mm:ss Z");
   //set the desired timezone
   formatter.setTimeZone(TimeZone.getTimeZone("Europe/London"));

   String formattedNowInTimeZone  = formatter.format(now);
   String formattedThenInTimeZone  = formatter.format(then);
Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
2

use SimpleDateFormat.setTimeZone(TimeZone) to set TimeZone.

    SimpleDateFormat sdf = new SimpleDateFormat("yourformat");   
    TimeZone tz = TimeZone.getDefault(); 
    sdf.setTimeZone(tz);
    sdf.format(yourdate); //will return a string rep of a date with the included format
Sam Bellerose
  • 1,782
  • 2
  • 18
  • 43
PermGenError
  • 45,977
  • 8
  • 87
  • 106
2

Date objects do not have a timezone, it is simply a container for a specific moment in time. If you want to apply a timezone to it, you'll have to use a Calendar. I do it as follows

Calendar cal = Calendar.getInstance();
cal.setTime( date );

If you're just looking to display the Date adjusted for a timezone, then you can use SimpleDateFormat to apply the proper timezone adjustments.

Khantahr
  • 8,156
  • 4
  • 37
  • 60
  • 1
    And how would i apply the actual timezone object to the calendar? – Johan Dec 03 '12 at 19:52
  • Do it with `cal.setTimeZone( TimeZone );`. Typically I'd use `cal.setTimeZone( TimeZone.getTimeZone( "Timezone String Id" );`. – Khantahr Dec 03 '12 at 20:02