display Java.util.Date in a specific format

后端 未结 10 1734
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 08:56

I have the following scenario :

SimpleDateFormat dateFormat = new SimpleDateFormat(\"dd/MM/yyyy\");
System.out.println(dateFormat.parse(\"31/05/2011\"));


        
相关标签:
10条回答
  • 2020-11-22 09:23

    It makes no sense, but:

    System.out.println(dateFormat.format(dateFormat.parse("31/05/2011")))
    
    SimpleDateFormat.parse() = // parse Date from String
    SimpleDateFormat.format() = // format Date into String
    
    0 讨论(0)
  • 2020-11-22 09:24

    This will help you. DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); print (df.format(new Date());

    0 讨论(0)
  • 2020-11-22 09:26

    If you want to simply output a date, just use the following:

    System.out.printf("Date: %1$te/%1$tm/%1$tY at %1$tH:%1$tM:%1$tS%n", new Date());
    

    As seen here. Or if you want to get the value into a String (for SQL building, for example) you can use:

    String formattedDate = String.format("%1$te/%1$tm/%1$tY", new Date());
    

    You can also customize your output by following the Java API on Date/Time conversions.

    0 讨论(0)
  • 2020-11-22 09:27

    You need to go through SimpleDateFormat.format in order to format the date as a string.

    Here's an example that goes from String -> Date -> String.

    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    Date date = dateFormat.parse("31/05/2011");
    
    System.out.println(dateFormat.format(date));   // prints 31/05/2011
    //                            ^^^^^^
    
    0 讨论(0)
提交回复
热议问题