Java日期格式转换 [英] Java Date Format Conversion

查看:123
本文介绍了Java日期格式转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在从请求对象中抽出三个表单域输入。一天,一个月和一年。

I am currently pulling three form field inputs off the request object. The day, the month, and the year.

当月11日当天
月份为12月12日
年将是2010年代表今年。

Day would be 11 for 11th day of the month Month would be 12 for december Year would be 2010 representing this year.

我需要将其转换为Java Date对象,但由于这么多变化,我不知道最好的方法将其存储在一个java对象中。我需要它的格式

I need to convert this into a Java Date object, but since so much has been changed, I am not sure what the best way to store this in a java object. I need it in the format

YYYY-MM-DD HH:MM:SS

所以我可以比较日期在MySql数据库中。

so I can compare to dates in the MySql Database.

推荐答案

SimpleDateFormat 可以将字符串转换为 java.util.Date 对象。
日期类有一个 getTime()方法,以unix时间格式生成日期(毫秒数自1970年1月1日00:00:00 GMT)。

SimpleDateFormat can convert strings to java.util.Date objects. Date class has a getTime() method that yields the date in unix time format (number of milliseconds since January 1, 1970, 00:00:00 GMT).

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
long unix_time = dateFormat.parse(date).getTime();

您可以在MySQL中使用 UNIX_TIMESTAMP()转换为unix时间。

You can use UNIX_TIMESTAMP() in MySQL to convert to unix time as well.

mysql> SELECT UNIX_TIMESTAMP('2007-11-30 10:30:19');
        -> 1196440219

这篇关于Java日期格式转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆