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

后端 未结 10 1278
南旧
南旧 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:16

    The following code gives expected output. Is that you want?

    import java.util.Calendar;
    import java.util.Date;
    
    public class DateAndTime {
    
        public static void main(String[] args) throws Exception {
            Calendar cal = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SS");
            String strDate = sdf.format(cal.getTime());
            System.out.println("Current date in String Format: " + strDate);
    
            SimpleDateFormat sdf1 = new SimpleDateFormat();
            sdf1.applyPattern("dd/MM/yyyy HH:mm:ss.SS");
            Date date = sdf1.parse(strDate);
            String string=sdf1.format(date);
            System.out.println("Current date in Date Format: " + string);
    
        }
    }
    

提交回复
热议问题