Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 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);
    System.out.println("Current date in Date Format: "+date);

}
}

and am getting following output

    Current date in String Format: 05/01/2012 21:10:17.287
    Current date in Date Format: Thu Jan 05 21:10:17 IST 2012

Kindly suggest what i should do to display the date in same string format(dd/MM/yyyy HH:mm:ss.SS) i.e i want following output:

    Current date in String Format: 05/01/2012 21:10:17.287
    Current date in Date Format: 05/01/2012 21:10:17.287

Kindly suggest

like image 688
Bharani Avatar asked Jan 05 '12 15:01

Bharani


People also ask

How can I get current date in dd mm yyyy format?

Format date with SimpleDateFormat('MM/dd/yy') in Java // displaying date Format f = new SimpleDateFormat("MM/dd/yy"); String strDate = f. format(new Date()); System. out. println("Current Date = "+strDate);

How can I get today date format?

const today = new Date(); function formatDate(date, format) { // } formatDate(today, 'mm/dd/yy'); You need to replace the strings "mm", "dd", "yy" with the respective month, day and year values from the format string passed in the argument. To do that you can use the replace() method like shown below: format.

How do I display a date in a specific format?

The dates appear as, mm/dd/yyyy in the U.S. and as, dd/mm/yyyy outside the U.S. where mm is the month, dd is the day, and yyyy is the year. The time is displayed as, hh:mm:ss AM/PM, where hh is the hour, mm is minutes, and ss is seconds.

What is SSS in date format?

SSS (3 S) is required for formating millisecond. i.e. "yyyy-MM-dd HH:mm:ss. SSS" is correct, but "yyyy-MM-dd HH:mm:ss.


1 Answers

SimpleDateFormat

sdf=new SimpleDateFormat("dd/MM/YYYY hh:mm:ss");
String dateString=sdf.format(date);

It will give the output 28/09/2013 09:57:19 as you expected.

For complete program click here

like image 54
user2822053 Avatar answered Sep 29 '22 08:09

user2822053