简体   繁体   English

从毫秒转换为MYSQL日期

[英]Convert from milliseconds to MYSQL Date

Hi I have a requirement to convert milliseconds to Date. 嗨我有要求将毫秒转换为日期。 Also the date should be acceptable by MYSQL. 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" 我试过这个例子但是在这里“sdf.format(calendar.getTime()”这个方法给出了以下格式的字符串。“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). 但我希望Date对象也是以上面的格式(yyyy-MM-dd HH:mm:ss)。

So how can I convert this to Date format. 那么如何将其转换为日期格式。

Please help me on this... 请帮帮我...

Thanx in advance... :) Thanx提前...... :)

We don't store dates or timestamps as a String in a database. 我们将日期或时间戳存储为数据库中的String 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. 您只需将它们保存为SQL Timestamp ,然后在需要显示或需要它们的字符串表示时,使用日期格式函数(无论是在Java中还是在后端使用PL / SQL )对它们进行格式化。

So, use java.sql.Timestamp as 因此,使用java.sql.Timestamp作为

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 编辑如果DOB字段是java.util.Date类型然后使用

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. 如果该字段的类型为java.sql.Date那么您可以保存它,如果后端列也是DATE类型,或者使用上面相同的代码将其首先转换为Timestamp

Date objects don't have a format. 日期对象没有格式。 That's why we need DateFormat objects to format them. 这就是我们需要DateFormat对象来格式化它们的原因。

Actually, you can't have date object in a particular format. 实际上,您不能以特定格式拥有日期对象。 Java manages date object internally. Java在内部管理日期对象。 However, you can format your date object whenever required using SimpleDateFormat . 但是,您可以使用SimpleDateFormat在需要时格式化日期对象。 Btw, there is no point in having Date object in a particular format. 顺便说一下,以特定格式使用Date对象毫无意义。

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")); System.out.println(getDate(82233213123L,“yyyy-MM-dd HH:mm:ss”));

out put should be Thu Aug 10 00:03:33 IST 1972 out put应该是8月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. 'dateObject'将根据您的需要提供格式的日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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