1

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?

Bishan
  • 15,211
  • 52
  • 164
  • 258
crazy_in_love
  • 145
  • 3
  • 13
  • 1
    Oracle's date/timestamp doesn't have a format. Do you mean that you want it to be as the `NLS_TIMESTAMP_FORMAT` ? or do you just need something like [this](http://stackoverflow.com/questions/3504986/java-date-time-format/3505033#3505033) ? – A.B.Cade Oct 21 '13 at 04:25

3 Answers3

7

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.

Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
3

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);
Kyle Colantonio
  • 454
  • 1
  • 3
  • 21
1

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);
Bishan
  • 15,211
  • 52
  • 164
  • 258