Java DateTimeFormatterBuilder with detailed timezone

Suppose I have a date, for example:

November 30, 2013 19:00: 00.001930000 Eastern Standard Time

I am trying to parse the input using DateTimeFormatterBuilder, but I cannot figure out what to put for the Setgeneric type ZoneId, which is lower null.

String basePattern = "MMM dd, yyyy HH:mm:ss";
new DateTimeFormatterBuilder()
        .appendPattern(basePattern)
        .appendFraction(ChronoField.NANO_OF_SECOND,0,9, true)
        .appendZoneText(TextStyle.FULL, null)
        .toFormatter();
+4
source share
1 answer

Pseudozones

, 3-4 , , . ! . , IST - . CST - , ( ).

. continent/region, America/Montreal, Africa/Casablanca Pacific/Auckland.

-, . , Locale . CST, Locale Locale.CHINA, , , CST " ", " " .

, . Locale . , , Locale , CST . , America/Chicago America/Winnipeg, CST, Locale.

Set< ZoneID > zones = new TreeSet<>() ;
zones.add( ZoneId.of( "America/Chicago" ) ;
zones.add( ZoneId.of( "America/Manitoba" ) ;
.appendZoneText( TextStyle.SHORT , zones ) 

, CST MacOS MacBook, America/Los_Angeles Locale.US. , appendZoneText (no Set).

String input = "Nov 30, 2013 19:00:00.001930000 CST";  
String basePattern = "MMM dd, yyyy HH:mm:ss";
DateTimeFormatter f = new DateTimeFormatterBuilder( )
        .appendPattern( basePattern )
        .appendFraction( ChronoField.NANO_OF_SECOND , 0 , 9 , true )
        .appendPattern( " " )
        .appendZoneText( TextStyle.SHORT  )
        .toFormatter( );

ZonedDateTime zdt = ZonedDateTime.parse( input , f );

System.out.println( "input: " + input );
System.out.println( "zdt.toString(): " + zdt );

: 30 2013 . 19:00: 00.001930000

zdt.toString(): 2013-11-30T19: 00: 00.001930-06: 00 [/]

Set ZoneId, , , CST China Standard Time. Set ZoneId. , .

Set < ZoneId > zones = new HashSet <>( );
zones.add( ZoneId.of( "Asia/Shanghai" ) ) ;

String input = "Nov 30, 2013 19:00:00.001930000 CST";  
String basePattern = "MMM dd, yyyy HH:mm:ss";
DateTimeFormatter f = new DateTimeFormatterBuilder( )
        .appendPattern( basePattern )
        .appendFraction( ChronoField.NANO_OF_SECOND , 0 , 9 , true )
        .appendPattern( " " )
        .appendZoneText( TextStyle.SHORT , zones )
        .toFormatter( );

ZonedDateTime zdt = ZonedDateTime.parse( input , f );

System.out.println( "input: " + input );
System.out.println( "zdt.toString(): " + zdt );

: 30 2013 . 19:00: 00.001930000

zdt.toString(): 2013-11-30T19: 00: 00.001930 + 08: 00 [/]

, , , . , , . , , , .

.appendZoneText( TextStyle.FULL ) 

:

String input = "Nov 30, 2013 19:00:00.001930000 Eastern Standard Time";  
String basePattern = "MMM dd, yyyy HH:mm:ss";
DateTimeFormatter f = new DateTimeFormatterBuilder( )
        .appendPattern( basePattern )
        .appendFraction( ChronoField.NANO_OF_SECOND , 0 , 9 , true )
        .appendPattern( " " )
        .appendZoneText( TextStyle.FULL )
        .toFormatter( );

ZonedDateTime zdt = ZonedDateTime.parse( input , f );

System.out.println( "input: " + input );
System.out.println( "zdt.toString(): " + zdt );

: 30 2013 . 19:00: 00.001930000

zdt.toString(): 2013-11-30T19: 00: 00.001930-05: 00 [America/New_York]

, Set ZoneId. Set ZonedDateTime. , America/New_York . , " ", , America/Nassau ..

, , . SortedSet , Set. , ZoneId Comparable, SortedSet, TreeSet.

Set < ZoneId > zones = new HashSet <>( );
zones.add( ZoneId.of( "America/Detroit" ) );
zones.add( ZoneId.of( "America/New_York" ) );
zones.add( ZoneId.of( "America/Nassau" ) );
zones.add( ZoneId.of( "America/Cancun" ) );

String input = "Nov 30, 2013 19:00:00.001930000 Eastern Standard Time";  
String basePattern = "MMM dd, yyyy HH:mm:ss";
DateTimeFormatter f = new DateTimeFormatterBuilder( )
        .appendPattern( basePattern )
        .appendFraction( ChronoField.NANO_OF_SECOND , 0 , 9 , true )
        .appendPattern( " " )
        .appendZoneText( TextStyle.FULL , zones )
        .toFormatter( );

ZonedDateTime zdt = ZonedDateTime.parse( input , f );

System.out.println( "input: " + input );
System.out.println( "zdt.toString(): " + zdt );

: 30 2013 . 19:00: 00.001930000

zdt.toString(): 2013-11-30T19: 00: 00.001930-06: 00 [/]

+6

Source: https://habr.com/ru/post/1687846/


All Articles