问题描述:

I want to convert a date object, ex: new Date(), to a string which has a format like Oracle's time stamp type, ex: 21-OCT-13 11.08.13.858000000 AM. I know I could just get each piece of information in the date object like day, month, year, hour, minute, ... to form the Oracle format string but I really want to know is there a utility to do that instead?

网友答案:

Try taking a look at SimpleDateFormats - That would be your best bet and easiest way of doing it.

Eg:

Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss"); //Hours:Minutes:Seconds
String strDate = dateFormat.format(date);
网友答案:

Using SimpleDateFormat#format() you would print a Date as

SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yy hh.mm.ss.SSSSSSSSS a");
System.out.println(sdf.format(new Date()).toUpperCase());

Output :

21-OCT-13 10.01.38.000000614 AM

See JavaDocs for Date and Time patterns.

网友答案:

Use SimpleDateFormat.

Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("your_format_here"); // dd/MM/yy h:mm:ss a
String formattedDate = sdf.format(date);
System.out.println(formattedDate);
相关阅读:
Top