This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

problem converting calendar to java.util.Date

By: a guest on Feb 21st, 2012  |  syntax: None  |  size: 0.86 KB  |  views: 4  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. TimeZone timeZone = TimeZone.getTimeZone("Australia/Sydney");
  2. GregorianCalendar defaultDate = new GregorianCalendar(timeZone);
  3. Date date = defaultDate.getTime();
  4.        
  5. Date d = new Date();
  6. DateFormat df = new SimpleDateFormat();
  7. df.setTimeZone(TimeZone.getTimeZone("Australia/Sydney"));
  8.  
  9. String formattedDate = df.format(d);   // returns Sydney date/time
  10. Date myDate = df.parse(formattedDate); // returns local time(ET)
  11.        
  12. import java.util.*;
  13. import java.text.*;
  14.  
  15. public class TzPrb {
  16.     public static void main(String[] args) {
  17.         Date d = new Date();
  18.         System.out.println(d);
  19.  
  20.         DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
  21.         df.setTimeZone(TimeZone.getTimeZone("Australia/Sydney"));
  22.         System.out.println(df.format(d));
  23.         df.setTimeZone(TimeZone.getTimeZone("Europe/London"));
  24.         System.out.println(df.format(d));
  25.     }
  26. }
clone this paste RAW Paste Data