mqsuresh
mqsuresh

Reputation: 5

TimeStamp patten

Calendar firstTime =Calendar.getInstance();

java.sql.Timestamp t1=new Timestamp(firstTime.getTime().getTime());

It is giving current Time like this

`2015-01-06 17:19:20.763`  

But i want timestamp pattern like below

'06-DEC-15 17:19:20.763000000 PM'

what do i do if i want to print timestamp pattern like this

could someone help me please

Upvotes: 0

Views: 562

Answers (2)

Cromax
Cromax

Reputation: 2052

If you strongly insist on the particular output format (1:1), you should use DateFormatSymbols additionally.

    final DateFormatSymbols dfs = new DateFormatSymbols();
    dfs.setShortMonths(new String[]{
        "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
        "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"});
    final DateFormat df = new SimpleDateFormat("dd-MMM-yy HH:mm:ss.SSS000000 a", dfs);
    System.out.println(df.format(new Timestamp(System.currentTimeMillis())));

And please remember, that DateFormat is not thread-safe.

Upvotes: 1

panagdu
panagdu

Reputation: 2133

You can use the SimpleDateFormat:

        Calendar firstTime = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat();
        df.applyPattern("dd-MMM-yy HH:mm:ss.SSS000000 aa");

        System.out.println(df.format(firstTime.getTimeInMillis()).toUpperCase());

I have put "0"s after SSS because the time you get is in miliseconds and it doesn't make any sense to have more than three "S"s but that's the requirement.

The toUpperCase is another requirement("Jan" was not enough).

Upvotes: 3

Related Questions