Java日期格式转换异常:YYYYMMDD转dd-mm-yyyy出错求助
Hey, let's break down why you're getting that weird 01-49-1970 output and how to fix it.
You've got two key mistakes here:
Treating the YYYYMMDD string as a timestamp directly
When you convert a string like"20240520"straight to along, you get the number20240520. But Java's date APIs (likeDateorInstant) use Unix timestamps—which are milliseconds since 1970-01-01 00:00:00 UTC.20240520milliseconds is only about 234 days, so that lands you right in early 1970. That's why your date is stuck in the epoch year.Using the wrong date format pattern
The49in01-49-1970tells me you probably usedDD(which means day of the year) instead ofdd(day of the month) in your formatter. For example, if you usednew SimpleDateFormat("MM-DD-YYYY"), the formatter would take the 49th day of 1970 and display it as01-49-1970(even though January only has 31 days—since it's counting the total days in the year, not the month).
You need to properly parse the YYYYMMDD string into a date object first, then convert that to a timestamp if you need a long. Here are two approaches, depending on your Java version:
Option 1: Use Java 8+'s java.time API (Recommended)
This is the modern, thread-safe way to handle dates—ditch the old SimpleDateFormat if you can:
import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.time.ZoneId; public class DateFix { public static void main(String[] args) { String dateInput = "20240520"; // Define the format matching your input string DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyyMMdd"); // Parse the string into a LocalDate (no time zone attached) LocalDate date = LocalDate.parse(dateInput, inputFormatter); // If you need a long timestamp (milliseconds since epoch), add a time zone and convert long timestamp = date.atStartOfDay(ZoneId.systemDefault()) .toInstant() .toEpochMilli(); // Verify with correct formatting DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("MM-dd-yyyy"); System.out.println(outputFormatter.format(date)); // Outputs 05-20-2024 System.out.println(timestamp); // Correct epoch millis for 2024-05-20 00:00:00 } }
Option 2: Use the Old SimpleDateFormat (Legacy Compatibility)
If you're stuck with Java 7 or older, be aware this class is not thread-safe:
import java.text.SimpleDateFormat; import java.util.Date; import java.text.ParseException; public class LegacyDateFix { public static void main(String[] args) throws ParseException { String dateInput = "20240520"; SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd"); // Parse the string to a Date object Date date = formatter.parse(dateInput); // Get the timestamp as a long long timestamp = date.getTime(); // Switch to the correct output format formatter.applyPattern("MM-dd-yyyy"); System.out.println(formatter.format(date)); // Outputs 05-20-2024 System.out.println(timestamp); } }
- Never convert YYYYMMDD strings directly to longs—they're not timestamps. Timestamps count milliseconds since 1970, while YYYYMMDD is just a numeric representation of the date.
- Watch your format characters:
yyyy= 4-digit year,MM= month,dd= day of month.YYYYis week-based year,DDis day of year—mixing these up causes weird outputs like you saw. - Stick with
java.timeif possible—it fixes almost all the flaws of the old date APIs.
内容的提问来源于stack exchange,提问作者oceano22

