Change string date format

my current date format is: 08/11/2008 00:00

I need to convert this output to 2008/11/08 00:00 However, using SimpleDateFormat, as has been investigated, it cannot do this and give me a completely different result, here are my codes: Follow:

SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy/MM/dd HH:mm")
Date starting= simpleDateFormat2.parse(startTime);
System.out.println("" + simpleDateFormat2.format(starting) + " real date " + startTime);

I know that I understand the correct line, given that the following output occurs:

0014/05/01 00:00 real date 08/11/2008 00:00

I'm not too sure how the mechanics 0014/05/01 00:00 are discovered instead

2008/11/08 00:00

I look forward to all suggestions Thank you in advance

+4
source share
4 answers

The first thing you need to do is parse the original value into an object Date

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);

, dateValue , ...

SimpleDateFormat output = new SimpleDateFormat("yyyy/MM/dd HH:mm");
System.out.println("" + output.format(dateValue) + " real date " + startTime);

:

2008/11/08 00:00 real date 08/11/2008 00:00

, 0014/05/01 00:00: SimpleDateFormat ( yyyy/MM/dd HH:mm) 08 , 11 2008 ,

+7

, , , , SimpleDateFormatter. MM/dd/yyyy HH:mm. SimpleDateFormat MM/dd/yyyy HH:mm.

.

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);
+3

.

    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));

2008/11/08 00:00

SimpleDateFormat, , , , . , . java.time API Java.

, : .

?

yyyy/MM/dd HH:mm 08/11/2008 2008- 11- 8- . SimpleDateFormat , 30 , , (2008 , 365,25, 5,5).

: java.time Java?

Java 6, .

  • Java 8 API.
  • Java 6 7 ThreeTen Backport, backport (ThreeTen JSR 310, API).
  • () Android Android ThreeTen Backport. ThreeTenABP. org.threeten.bp .

+1

You parse a date 08/11/2008 00:00with a format yyyy/MM/dd HH:mm, but that date does not match in that format.

0
source

Source: https://habr.com/ru/post/1624707/


All Articles