display Java.util.Date in a specific format

后端 未结 10 1904
没有蜡笔的小新
没有蜡笔的小新 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: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.

提交回复
热议问题