Learn how to accurately convert a string date to milliseconds using Joda Time in Java, ensuring the correct year is parsed.
---
This video is based on the question stackoverflow.com/q/67044503/ asked by the user 'alfa beta' ( stackoverflow.com/u/5950594/ ) and on the answer stackoverflow.com/a/67044588/ provided by the user 'Andreas' ( stackoverflow.com/u/5221149/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: How to convert a string date to millis with Joda (java)?
Also, Content (except music) licensed under CC BY-SA meta.stackexchange.com/help/licensing
The original Question post is licensed under the 'CC BY-SA 4.0' ( creativecommons.org/licenses/by-sa/4.0/ ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( creativecommons.org/licenses/by-sa/4.0/ ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
How to Convert a String Date to Millis with Joda in Java: A Simple Guide
When working with dates in Java, you might encounter scenarios where you need to convert a date string into milliseconds since epoch. This is particularly common in applications that deal with date manipulations, logging, or data storage. One common library used for date and time handling in Java is Joda-Time.
In this post, we'll discuss a specific issue that arises while converting date strings using Joda and how you can solve it effortlessly.
The Problem: Incorrect Parsing of Date Strings
Let’s consider an example where you have the following date string:
[[See Video to Reveal this Text or Code Snippet]]
You want to convert this string into milliseconds. However, your code returns a negative number when using the following parsing approach:
[[See Video to Reveal this Text or Code Snippet]]
Instead of a valid millisecond timestamp, you're receiving a value like -61495771847000, which is puzzling.
Understanding the Issue
The negative value returned from your code indicates that the date parsing is incorrect. To understand why this happens, let’s break it down step by step:
Date Format Mismatch: The pattern specified uses yyyy, which indicates a four-digit year. However, your date string provides a two-digit year (21), which is being misinterpreted. Consequently, the parser treats it as the year 0021 instead of 2021.
The Solution: Correcting the Date Format
To fix this issue, you should adjust the date pattern you're using. Instead of specifying the year with yyyy, you should use yy, which effectively interprets a two-digit year within the context of a century.
Here’s the corrected code:
[[See Video to Reveal this Text or Code Snippet]]
What Happens Next?
By using yy in your date pattern:
The string "Sun, 11 Apr 21 09:09:13 + 0000" will be correctly interpreted as April 11, 2021.
The result returned will be a valid positive long value representing the milliseconds since the epoch (January 1, 1970).
After this modification, you should see a result like 1618132153000, which corresponds to the expected timestamp for April 11, 2021.
Conclusion
Working with dates in Java can sometimes lead to unexpected results due to formatting issues. By carefully matching your date patterns with the configurations of your date strings, you can avoid pitfalls like negative timestamps.
We hope this guide has helped you understand how to efficiently convert a string date to milliseconds using Joda-Time in Java. Happy coding!
コメント