简体   繁体   中英

Java util calendar - is it enough to set timezone to get time in another timezone

I need to get hours value of Calendar time in another timezone. I tried this example: https://stackoverflow.com/a/7695910/775523 (java.util.Calendar section) but it prints 15 for the last cal.get(Calendar.HOUR_OF_DAY) (I expect to have 17!). So what is a correct way to get number of hours in another timezone - I use build 1.7.0_21-b11. Thanks.

you can use this code reference

    Date date = new Date();  
    DateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");          
    formatter.setTimeZone(TimeZone.getTimeZone("Asia/Tokyo"));  
    // Prints the date in the CET timezone  
    System.out.println(formatter.format(date));  
    // Set the formatter to use a different timezone  
    formatter.setTimeZone(TimeZone.getTimeZone("Australia/Melbourne"));  
    // Prints the date in the IST timezone  
    System.out.println(formatter.format(date));

you can use date.getHours();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM