For example, why did the date change here?
2010-09-23 17:27:54,565 DEBUG [SomeClass] start date Thu Sep 09 00:00:00 UTC 2010 ... 2010-09-23 10:44:24,728 DEBUG [SomeClass] start date Wed Sep 08 17:00:00 PDT 2010
At a glance it looks like the date isn't the same: it isn't the same day, hour, or timezone!
The answer is the first line of the Date class javadoc:
"The class Date represents a specific instant in time, with millisecond precision." (see here)Note that it does NOT have a timezone. A timezone is applied only when formatting the date. We can even prove the date hasn't changed:
import static org.junit.Assert.assertEquals; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import org.junit.Test; public class DateTzTest { @Test public void dateCompare() throws ParseException { DateFormat df = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy"); Date d1 = df.parse("Thu Sep 09 00:00:00 UTC 2010"); Date d2 = df.parse("Wed Sep 08 17:00:00 PDT 2010"); assertEquals(d1.getTime(), d2.getTime()); } }
2 comments:
Fantastic read! Your exploration of timezone issues is both timely and relatable, especially for developers working with global applications. The examples you provided highlight the common pitfalls many encounter when dealing with date and time manipulation. I appreciate the practical tips you shared for managing timezones effectively. It would be interesting to see a discussion on how different programming languages handle timezone conversions and any best practices for avoiding common errors. Thanks for sharing this insightful piece—it's a valuable resource for anyone grappling with timezone challenges!
Digital Marketing Course In Hyderabad
This article provides an insightful introduction to the complexities of handling time zones in Scala. I appreciate how you highlighted the importance of understanding local time vs. UTC and the potential pitfalls that can arise when working with time-related data. It’s crucial for developers to grasp these concepts to avoid bugs in applications that rely heavily on accurate timekeeping. Thanks for sharing your knowledge on this topic!
Digital Marketing Course In Ameerpet
Post a Comment