want current date and time in “dd/MM/yyyy HH:mm:ss.SS” format

后端 未结 10 1282
南旧
南旧 2020-11-22 01:31

I am using following code to get date in \"dd/MM/yyyy HH:mm:ss.SS\" format.

    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import          


        
10条回答
  •  伪装坚强ぢ
    2020-11-22 02:09

    If you are using JAVA8 API then this code will help.

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    String dateTimeString = LocalDateTime.now().format(formatter);
    System.out.println(dateTimeString);
    

    It will print the date in the given format.

    But if you again create a object of LocalDateTime it will print the 'T' in between the date and time.

    LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, formatter);
    System.out.println(dateTime.toString());
    

    So as mentioned in earlier posts as well, the representation and usage is different.

    Its better to use "yyyy-MM-dd'T'HH:mm:ss" pattern and convert the string/date object accordingly.

提交回复
热议问题