Timezone conversion

后端 未结 12 1539
一向
一向 2020-11-22 11:25

I need to convert from one timezone to another timezone in my project.

I am able to convert from my current timezone to another but not from a different timezone to

12条回答
  •  余生分开走
    2020-11-22 12:02

    Depends on what you really mean by "converting".

    It MAY be as simple as setting the time zone in the FORMATTER, and not mucking with Calendar at all.

    Calendar cal = Calendar.getInstance();
    
    TimeZone tzUTC = TimeZone.getTimeZone( "UTC" );
    TimeZone tzPST = TimeZone.getTimeZone( "PST8PDT" );
    
    DateFormat dtfmt = new SimpleDateFormat( "EEE, yyyy-MM-dd KK:mm a z" );
    
    dtfmt.setTimeZone( tzUTC );
    System.out.println( "UTC: " + dtfmt.format( cal.getTime() ));
    
    dtfmt.setTimeZone( tzPST );
    System.out.println( "PST: " + dtfmt.format( cal.getTime() ));
    

提交回复
热议问题