How to convert string “2011-11-29 12:34:25” to date in “dd-MM-yyyy” format in JAVA

前端 未结 6 844
死守一世寂寞
死守一世寂寞 2020-12-21 15:30

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.

相关标签:
6条回答
  • 2020-12-21 16:10

    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

    0 讨论(0)
  • 2020-12-21 16:18

    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);
    
    0 讨论(0)
  • 2020-12-21 16:24

    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.

    0 讨论(0)
  • 2020-12-21 16:30

    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);
    
    0 讨论(0)
  • 2020-12-21 16:32

    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.

    0 讨论(0)
  • 2020-12-21 16:32

    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
    }
    
    0 讨论(0)
提交回复
热议问题