Wherefore is subtracting these 2 epoch-milli Occasions (successful twelvemonth 1927) giving a unusual consequence?

Wherefore is subtracting these 2 epoch-milli Occasions (successful twelvemonth 1927) giving a unusual consequence?

If I tally the pursuing programme, which parses 2 day strings referencing instances 1 2nd isolated and compares them:

public static void main(String[] args) throws ParseException { SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String str3 = "1927-12-31 23:54:07"; String str4 = "1927-12-31 23:54:08"; Date sDt3 = sf.parse(str3); Date sDt4 = sf.parse(str4); long ld3 = sDt3.getTime() /1000; long ld4 = sDt4.getTime() /1000; System.out.println(ld4-ld3);}

The output is:

353

Wherefore is ld4-ld3, not 1 (arsenic I would anticipate from the 1-2nd quality successful the instances), however 353?

If I alteration the dates to instances 1 2nd future:

String str3 = "1927-12-31 23:54:08"; String str4 = "1927-12-31 23:54:09"; 

Past ld4-ld3 volition beryllium 1.


Java interpretation:

java version "1.6.0_22"Java(TM) SE Runtime Environment (build 1.6.0_22-b04)Dynamic Code Evolution Client VM (build 0.2-b02-internal, 19.0-b04-internal, mixed mode)
Timezone(`TimeZone.getDefault()`):sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null]Locale(Locale.getDefault()): zh_CN

It's a clip region alteration connected December Thirty first successful Shanghai.

Seat this leaf for particulars of 1927 successful Shanghai. Fundamentally astatine midnight astatine the extremity of 1927, the clocks went backmost 5 minutes and Fifty two seconds. Truthful "1927-12-31 23:Fifty four:08" really occurred doubly, and it seems similar Java is parsing it arsenic the future imaginable immediate for that section day/clip - therefore the quality.

Conscionable different occurrence successful the frequently bizarre and fantastic planet of clip zones.

If rebuilt with interpretation 2013a of TZDB, The first motion would nary longer show rather the aforesaid behaviour. Successful 2013a, the consequence would beryllium 358 seconds, with a modulation clip of 23:Fifty four:03 alternatively of 23:Fifty four:08.

I lone observed this due to the fact that I'm accumulating questions similar this successful Noda Clip, successful the signifier of part checks... The trial has present been modified, however it conscionable goes to entertainment - not equal humanities information is harmless.

Successful TZDB 2014f, the clip of the alteration has moved to 1900-12-31, and it's present a specified 343 2nd alteration (truthful the clip betwixt t and t+1 is 344 seconds, if you seat what I average).


To reply a motion about a modulation astatine 1900... it seems similar the Java clip region implementation treats each clip zones arsenic merely being successful their modular clip for immoderate immediate earlier the commencement of 1900 UTC:

import java.util.TimeZone;public class Test { public static void main(String[] args) throws Exception { long startOf1900Utc = -2208988800000L; for (String id : TimeZone.getAvailableIDs()) { TimeZone zone = TimeZone.getTimeZone(id); if (zone.getRawOffset() != zone.getOffset(startOf1900Utc - 1)) { System.out.println(id); } } }}

The codification supra produces nary output connected my Home windows device. Truthful immoderate clip region which has immoderate offset another than its modular 1 astatine the commencement of 1900 volition number that arsenic a modulation. TZDB itself has any information going backmost earlier than that, and doesn't trust connected immoderate thought of a "fastened" modular clip (which is what getRawOffset assumes to beryllium a legitimate conception) truthful another libraries needn't present this man-made modulation.


You've encountered a section clip discontinuity:

Once section modular clip was astir to range Sunday, 1. January 1928, 00:00:00 clocks have been turned backward Zero:05:Fifty two hours to Saturday, 31. December 1927, 23:Fifty four:08 section modular clip alternatively

This is not peculiarly unusual and has occurred beautiful overmuch everyplace astatine 1 clip oregon different arsenic timezones have been switched oregon modified owed to governmental oregon administrative actions.


Knowing however Java handles dates and occasions, particularly with epoch milliseconds, tin generally pb to sudden outcomes, peculiarly once dealing with humanities dates. The intricacies of clip zones, daylight redeeming clip (DST), and the manner Java's Day and Calendar courses run tin origin calculations involving dates from the aboriginal Twentieth period to behave successful methods that mightiness look counterintuitive astatine archetypal glimpse. This article delves into wherefore subtracting 2 epoch-milli events, particularly these about the palmy twelvemonth of 1927, mightiness output a peculiar result, exploring the nuances of Java's day-clip dealing with and offering insights into possible causes and options.

Wherefore Mightiness Subtracting Epoch Milliseconds About 1927 Springiness a Unusual Consequence?

Subtracting epoch milliseconds ought to, successful explanation, supply a simple quality successful clip. Nevertheless, once dealing with dates about 1927, particularly successful circumstantial geographical areas, humanities adjustments successful clip region guidelines travel into drama. Galore areas adjusted their clip region offsets oregon carried out daylight redeeming clip for the archetypal clip about this play. These adjustments tin present discrepancies once you person dates to epoch milliseconds, arsenic the conversion relies upon connected the clip region accusation progressive astatine that circumstantial minute successful past. This means that the figure of milliseconds representing a peculiar day tin beryllium affected by these humanities clip region regulation adjustments, starring to sudden variations once performing subtractions.

Contact of Clip Region Past connected Epoch Millisecond Calculations

The epoch millisecond worth represents the figure of milliseconds that person elapsed since the opening of the Unix epoch, which is January 1, 1970, 00:00:00 UTC. Changing a circumstantial day to its epoch millisecond cooperation entails figuring out the offset from UTC for that day's clip region. If the clip region offset for a peculiar part modified importantly about 1927, the epoch millisecond worth for dates successful that play volition indicate these adjustments. Nevertheless bash I revert a Git repository to a erstwhile perpetrate? This tin consequence successful the quality betwixt 2 epoch millisecond values not straight corresponding to the intuitive quality successful calendar clip. To precisely cipher clip variations involving humanities dates, it's important to relationship for these clip region variations.

However to Grip Clip Region Adjustments successful Java Day Calculations

To precisely cipher clip variations involving dates from durations with important clip region adjustments, specified arsenic 1927, you ought to make the most of Java's java.clip bundle, launched successful Java Eight. This bundle supplies much exact and dependable courses for day and clip manipulation in contrast to the bequest java.util.Day and java.util.Calendar courses. Particularly, usage ZonedDateTime to correspond dates and occasions with clip region accusation. Once performing calculations, guarantee you are utilizing the accurate clip region ID and that you are alert of immoderate humanities clip region regulation adjustments that mightiness impact your outcomes. Present's an illustration:

  import java.time.ZonedDateTime; import java.time.ZoneId; import java.time.Duration; public class TimeZoneCalculation { public static void main(String[] args) { ZoneId zoneId = ZoneId.of("America/Los_Angeles"); ZonedDateTime date1 = ZonedDateTime.of(1927, 1, 1, 0, 0, 0, 0, zoneId); ZonedDateTime date2 = ZonedDateTime.of(1927, 1, 2, 0, 0, 0, 0, zoneId); Duration duration = Duration.between(date1, date2); System.out.println("Difference in days: " + duration.toDays()); } }  

Utilizing ZonedDateTime and Length from the java.clip bundle helps relationship for clip region variations and daylight redeeming clip transitions, offering a much close cooperation of the clip quality betwixt 2 dates. Usage respected sources similar the IANA Clip Region Database for the about ahead-to-day clip region accusation. Ever validate your calculations towards identified humanities information to guarantee accuracy. For further assets, see exploring Oracle's Java documentation for champion practices successful day and clip dealing with. And beryllium certain to cheque retired the Joda-Clip room for blanket day and clip performance.

Evaluating Day and Clip Courses successful Java

Characteristic java.util.Day & Calendar (Bequest) java.clip (Java Eight+)
Immutability Mutable (not thread-harmless) Immutable (thread-harmless)
API Plan Analyzable and mistake-inclined Broad and intuitive
Clip Region Dealing with Mediocre, depends connected java.util.TimeZone Fantabulous, makes use of java.clip.ZoneId and java.clip.ZonedDateTime
Precision Milliseconds Nanoseconds
Humanities Clip Region Information Constricted and frequently inaccurate Blanket and ahead-to-day (depends connected IANA database)

Arsenic the array illustrates, the java.clip bundle provides important benefits complete the older java.util.Day and java.util.Calendar courses, particularly once dealing with clip zones and humanities dates. Ever like utilizing java.clip for fresh tasks and see migrating present codification to return vantage of its improved options and accuracy.

Successful decision, subtracting epoch milliseconds from dates about 1927 tin output different outcomes owed to humanities clip region adjustments and however Java's bequest day-clip courses grip these adjustments. By utilizing the java.clip bundle and being conscious of clip region past, you tin execute close day calculations and debar sudden outcomes. Clasp contemporary Java day and clip APIs to guarantee your functions appropriately grip humanities dates and clip region transitions, starring to much dependable and predictable outcomes. Return the clip to improve your functions to usage java.clip present!


Previous Post Next Post

Formulario de contacto