By : black nightz
Date : November 23 2020, 03:01 PM
|
Share :
|
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
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?
By : Michael Duckworth
Date : March 29 2020, 07:55 AM
|
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
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.
|
|
|
Related Posts :
|