Amar
Amar

Reputation: 763

Convert from milliseconds to MYSQL Date

Hi I have a requirement to convert milliseconds to Date. Also the date should be acceptable by MYSQL.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("US/Central"));
calendar.setTimeInMillis(System.currentTimeMillis());
System.out.println("GregorianCalendar -" + sdf.format(calendar.getTime()));

I tried this example but here "sdf.format(calendar.getTime()" this method its giving string in the below format. "yyyy-MM-dd HH:mm:ss"

But I want Date object also that to be in the above format (yyyy-MM-dd HH:mm:ss).

So how can I convert this to Date format.

Please help me on this...

Thanx in advance... :)

Upvotes: 0

Views: 2925

Answers (5)

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51721

We don't store dates or timestamps as a String in a database. Hence, saving in a particular format doesn't make sense. You just need to save them as a SQL Timestamp and then format them using Date format functions (be it in Java or at the back end using PL/SQL) whenever you need to display or need a String representation of them.

So, use java.sql.Timestamp as

Timestamp dbDateTime = new java.sql.Timestamp(System.currentTimeMillis()); // or
Timestamp dbDateTime = new java.sql.Timestamp(calendar.getTimeInMillis());

EDIT : If the DOB field is of type java.util.Date then use

Timestamp dbDateTime = new java.sql.Timestamp(dob.getTime());

If the field is of type java.sql.Date then you can either save it as it is if the backend column is also of type DATE or use the same code above to convert it into a Timestamp first.

Upvotes: 2

Niju
Niju

Reputation: 487

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("US/Central"));
calendar.setTimeInMillis(System.currentTimeMillis());
String date =  sdf.format(calendar.getTime());
Date dateObject = sdf.parse(date);

The 'dateObject' will give you the date in the format as you need.

Upvotes: 0

Devi Kiran
Devi Kiran

Reputation: 598

public static Date getDate(long milliSeconds, String dateFormat)
{
DateFormat formatter = new SimpleDateFormat(dateFormat);

 Calendar calendar = Calendar.getInstance();
 calendar.setTimeInMillis(milliSeconds);
 DateFormat formatter2 = new SimpleDateFormat(dateFormat);
 Date d = null;
try {
    d = (Date)formatter2.parse(formatter.format(calendar.getTime()));
    System.out.println(formatter.format(calendar.getTime()));
} catch (ParseException e) {
    e.printStackTrace();
}
 return d;
}

try accessing it like this

System.out.println(getDate(82233213123L, "yyyy-MM-dd HH:mm:ss"));

out put should be Thu Aug 10 00:03:33 IST 1972

Upvotes: 0

Darshan Mehta
Darshan Mehta

Reputation: 30849

Actually, you can't have date object in a particular format. Java manages date object internally. However, you can format your date object whenever required using SimpleDateFormat. Btw, there is no point in having Date object in a particular format.

Upvotes: 0

Joni
Joni

Reputation: 111389

Date objects don't have a format. That's why we need DateFormat objects to format them.

Upvotes: 0

Related Questions