logo
down
shadow

Java DateTimeFormatterBuilder with Verbose Time Zone


Java DateTimeFormatterBuilder with Verbose Time Zone

By : black nightz
Date : November 23 2020, 03:01 PM
this one helps. Pseudo zones
As the class documentation explains briefly, the 3-4 letters commonly seen in mainstream media to indicate a time zone are not actually official time zones. These pseudo-zones are not standardized, and are not even unique! Many codes are re-used around the globe. For example, IST is both India Standard Time and Irish Standard Time. And CST is both China Standard Time and Central Standard Time (in North America).
code :
Set< ZoneID > zones = new TreeSet<>() ;
zones.add( ZoneId.of( "America/Chicago" ) ;
zones.add( ZoneId.of( "America/Manitoba" ) ;
…
.appendZoneText( TextStyle.SHORT , zones ) 
…
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 );
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 );
.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 );
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 );


Share : facebook icon twitter icon
converting date from one time zone to other time zone in java

converting date from one time zone to other time zone in java


By : Roy
Date : March 29 2020, 07:55 AM
should help you out tl;dr
Use modern java.time classes, specifically ZonedDateTime and ZoneId. See Oracle Tutorial.
code :
ZonedDateTime                         // Represent a date and time-of-day in a specific time zone.
.now(                                 // Capture the current moment as seen in the wall-clock time used by the people of a particular region (a time zone). 
    ZoneId.of( "Pacific/Auckland" )   // Specify time zone using proper name in `Continent/Region` format. Never use 3-4 letter pseudo-zone such as IST or PST or EST.
)                                     // Returns a `ZonedDateTime` object.
.withZoneSameInstant(                 // Adjust from one time zone to another. Same point on the timeline, same moment, but different wall-clock time.
    ZoneId.of( "Africa/Tunis" )       
)                                     // Returns a new fresh `ZonedDateTime` object rather than altering/“mutating” the original, per immutable objects pattern.
.toString()                           // Generate text in standard ISO 8601 format, extended to append name of zone in square brackets.
Instant instant = Instant.now() ;  // Capture the current moment in UTC.
ZoneId zMontreal = ZoneId.of( "America/Montreal" ) ;  
ZonedDateTime zdtMontreal = instant.atZone( zMontreal ) ;  // Same moment, different wall-clock time.
ZoneId zKolkata = ZoneId.of( "Asia/Kolkata" ) ;
ZonedDateTime zdtKolkata = zdtMontreal.withZoneSameInstant​( zKolkata ) ;
Instant instant = Instant.parse( "2018-01-23T01:23:45.123456Z" ) ;
Instant instant = zdtKolkata.toInstant() ;
//DateTime now = new DateTime(); // Default time zone automatically assigned.

// Convert a java.util.Date to Joda-Time.
java.util.Date date = new java.util.Date();
DateTime now = new DateTime( date );  // Default time zone automatically assigned.

DateTimeZone timeZone = DateTimeZone.forID( "Asia/Kolkata" );
DateTime nowIndia = now.toDateTime( timeZone );

// For UTC/GMT, use built-in constant.
DateTime nowUtcGmt = nowIndia.toDateTime( DateTimeZone.UTC );

// Convert from Joda-Time to java.util.Date.
java.util.Date date2 = nowIndia.toDate();
System.out.println( "date: " + date );
System.out.println( "now: " + now );
System.out.println( "nowIndia: " + nowIndia );
System.out.println( "nowUtcGmt: " + nowUtcGmt );
System.out.println( "date2: " + date2 );
date: Sat Jan 25 16:52:28 PST 2014
now: 2014-01-25T16:52:28.003-08:00
nowIndia: 2014-01-26T06:22:28.003+05:30
nowUtcGmt: 2014-01-26T00:52:28.003Z
date2: Sat Jan 25 16:52:28 PST 2014
Convert Time Zone from a String to a particular Time Zone taking DST into consideration of target Time Zone

Convert Time Zone from a String to a particular Time Zone taking DST into consideration of target Time Zone


By : Gangrel
Date : March 29 2020, 07:55 AM
should help you out I am trying to convert a date string in format "yyyyMMddHHmmss" to a date string for another Time Zone "America/Sao_Paulo" taking DST into consideration for America/Sao_Paulo ( which start on 16-oct-2016 ). , This worked for me
code :
try
{
SimpleDateFormat f = new SimpleDateFormat("yyyyMMddhhmmss");
f.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));
Date time = f.parse("20161015113634");  


DateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");  
formatter.setTimeZone(TimeZone.getTimeZone("America/Sao_Paulo"));

System.out.println(formatter.format(time)); 
}
catch (ParseException e){

} 
How do I specify a zone offset in a DateTimeFormatterBuilder without using a pattern?

How do I specify a zone offset in a DateTimeFormatterBuilder without using a pattern?


By : Michael Duckworth
Date : March 29 2020, 07:55 AM
Hope that helps You can use appendOffset rather than appendOffsetId. The first argument is a pattern, but not a DateTimeFormatter pattern; in fact it is limited to 22 possible String values:
code :
.appendOffset("+HHMM", "????")
Java 8 time zone ZoneRulesException: Unknown time-zone ID: EST

Java 8 time zone ZoneRulesException: Unknown time-zone ID: EST


By : user3084841
Date : December 27 2020, 04:20 PM
should help you out You are mixing old and new API's.
TimeZone.getAvailableIDs() returns Time Zone ID's that TimeZone.getTimeZone(String ID) can resolve.
code :
public static void main(String[] args) {
    Set<String> timeZones = Set.of(TimeZone.getAvailableIDs());
    Set<String> zoneIds = ZoneId.getAvailableZoneIds();
    System.out.println("Extra TimeZone's: " + diff(timeZones, zoneIds));
    System.out.println("Extra ZoneId's: " + diff(zoneIds, timeZones));
}
static Set<String> diff(Set<String> a, Set<String> b) {
    Set<String> diff = new TreeSet<>(a);
    diff.removeAll(b);
    return diff;
}
Extra TimeZone's: [ACT, AET, AGT, ART, AST, BET, BST, CAT, CNT, CST, CTT, EAT, ECT, EST, HST, IET, IST, JST, MIT, MST, NET, NST, PLT, PNT, PRT, PST, SST, VST]
Extra ZoneId's: []
Java how to get list of time zone ID’s for the given time zone abbreviation

Java how to get list of time zone ID’s for the given time zone abbreviation


By : Saroj Raut
Date : March 29 2020, 07:55 AM
will help you Interesting question. Since the abbreviations aren’t standardized, there cannot be an authoritative answer nor a bulletproof way to get such an answer. Off the top of my head I thought of two approaches:
Get them from your JVM. Find them on the net.
shadow
Privacy Policy - Terms - Contact Us © voile276.org