How to convert string “2011-11-29 12:34:25” to dat

2020-02-15 01:46发布

问题:

I am trying to convert date which is in string and got format of "yyyy-MM-dd HH:mm:ss" to "dd-MM-yyyy".

I have implmented following code but its giving : java.lang.IllegalArgumentException

        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        Date date = new Date(values);
        String mydate = dateFormat.format(date);

回答1:

First you have to parse the string representation of your date-time into a Date object.

DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = (Date)formatter.parse("2011-11-29 12:34:25");

Then you format the Date object back into a String in your preferred format.

DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
String mydate = dateFormat.format(date);


回答2:

You need to parse the date, using another SimpleDateFormat

SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = dateFormat2.parse(values);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String mydate = dateFormat.format(date);


回答3:

tl;dr

LocalDateTime.parse( 
    "2011-11-29 12:34:25".replace( " " , "T " ) 
).format (
    DateTimeFormatter.ofPattern( "dd-MM-uuuu" )
)

29-11-2011

Using java.time

The modern approach uses the java.time classes rather than the troublesome legacy classes.

ISO 8601

Your input string of “2011-11-29 12:34:25” has a format nearly that of the ISO 8601 standard. To fully comply, replace the SPACE in the middle with a T.

String input = "2011-11-29 12:34:25".replace( " " , "T " );

Without any indication of time zone or offset-from-UTC, we parse as a LocalDateTime.

LocalDateTime ldt = LocalDateTime.parse( input ) ;

You want the date-only value, so extract a LocalDate. The LocalDate class represents a date-only value without time-of-day and without time zone.

LocalDate ld = ldt.toLocalDate() ;

To generate a string in your desired format, you specify a custom formatting pattern.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd-MM-uuuu" );
String output = ld.format( f );

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

  • Java SE 8, Java SE 9, and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
    • See How to use ThreeTenABP….

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.



回答4:

The IllegalArgumentException probably occurs when program trying to construct Date object

What's the value ?

Following code snippet runs correctly.

SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date date = new Date();
String mydate = dateFormat.format(date);

System.out.println(mydate);

Output :

29/11/2011



回答5:

The constructor to Date takes a long representing the time in milliseconds. You need to use another SimpleDateFormat instance to parse your input string into a timestamp first.



回答6:

try this way

String mydate = "2011-11-29 12:34:25"
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = null;

try{
   date = sdf.parse(mydate);
   SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
   String mydate = dateFormat.format(date);
}catch(Exception ex){
   // handle exception
}