my current date format is : 08/11/2008 00:00
I need to convert this output to 2008/11/08 00:00 However, using the SimpleDateFormat as researched it is unable to do so and give me a totally different output, here are my codes as Follows :
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy/MM/dd HH:mm")
Date starting= simpleDateFormat2.parse(startTime);
System.out.println("" + simpleDateFormat2.format(starting) + " real date " + startTime);
i do know that i am parsing in the right string given that the following output occurs :
0014/05/01 00:00 real date 08/11/2008 00:00
i am not too sure about how as to the mechanics detected 0014/05/01 00:00 instead of
2008/11/08 00:00
i look forward to all sugguestions Thanks in advance
The first thing you need to do is parse the original value to a Date
object
String startTime = "08/11/2008 00:00";
// This could be MM/dd/yyyy, you original value is ambiguous
SimpleDateFormat input = new SimpleDateFormat("dd/MM/yyyy HH:mm");
Date dateValue = input.parse(startTime);
Once you have that done, you can format the dateValue
any way you want...
SimpleDateFormat output = new SimpleDateFormat("yyyy/MM/dd HH:mm");
System.out.println("" + output.format(dateValue) + " real date " + startTime);
which outputs:
2008/11/08 00:00 real date 08/11/2008 00:00
The reason you're getting 0014/05/01 00:00
is SimpleDateFormat
(when using yyyy/MM/dd HH:mm
) is using 08
for the year, 11
for the month and 2008
for the day, it's doing an internal rolling of the values to correct the values to a valid date
I should like to contribute the modern answer.
DateTimeFormatter currentFormatter = DateTimeFormatter.ofPattern("dd/MM/uuuu HH:mm");
DateTimeFormatter convertedOutputFormatter = DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm");
String startTime = "08/11/2008 00:00";
LocalDateTime starting = LocalDateTime.parse(startTime, currentFormatter);
System.out.println(starting.format(convertedOutputFormatter));
This prints
2008/11/08 00:00
The SimpleDateFormat
class you tried to use is long outdated and notoriously troublesome. You have experienced but a little of the trouble with it. Instead I am using and recommending java.time
, the modern Java date and time API.
As others have said, you need to use two formatters, one for specifying your current format and one for specifying the desired format.
Your formatter with format pattern yyyy/MM/dd HH:mm
interpreted 08/11/2008 as the 2008th day of the 11th month of year 8 of the common era. It doesn’t disturb a SimpleDateFormat
with default settings that there are only 30 days in November, it just continues counting into the following months and years, ending up on a date five and a half years later (2008 divided by 365.25 is very close to 5.5).
java.time
with my Java version?If using at least Java 6, you can.
org.threeten.bp
and subpackages.java.time
.It is because the source date string you parsed is in a different format than your SimpleDateFormatter. Your source is in MM/dd/yyyy HH:mm
format. Change the string in the SimpleDateFormat to MM/dd/yyyy HH:mm
.
Try using another format after your first format.
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("MM/dd/yyyy HH:mm");
//You gotta parse it to a Date before correcting it
Date parsedDate = simpleDateFormat2.parse(startTime);
simpleDateFormat2 = new SimpleDateFormat("yyyy/MM/dd HH:mm")
String newFormatttedDate = simpleDateFormat2.format(parsedDate);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With