Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get date and time in java like this 02-Oct-12 12:58:20 AM

Tags:

java

datetime

i have tried some thing like this

package com.poc;

import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;


public class GetCurrentDateTime {
  public static void main(String[] args) {

       DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       //get current date time with Date()
       Date date = new Date();
       System.out.println(dateFormat.format(date));

       //get current date time with Calendar()
       Calendar cal = Calendar.getInstance();
       System.out.println(dateFormat.format(cal.getTime()));

  }
}

and my out put is : 2013-01-07 17:12:27 but want the output to be like this 2013-Jan-07 17:12:27 PM

how to do this in java?

Best Regards

like image 443
Java Questions Avatar asked Dec 04 '25 17:12

Java Questions


1 Answers

your format should be

new SimpleDateFormat("dd-MMM-yy hh:mm:ss a");

07-Jan-13 07:20:02 PM

new SimpleDateFormat("dd-MMM-yy HH:mm:ss");

07-Jan-13 19:20:02

like image 169
Naveenkumar K Avatar answered Dec 06 '25 07:12

Naveenkumar K