简体   繁体   中英

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... :)

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.

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

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM