java里时间的转换
备注:从页面获取时间类型的值,String转换为Date,再从java.util.Date转为java.sql.Date,或者java.sql.Timestamp转为数据库中的datetime类型。发现代码变得比较凌乱,考虑不想使用第三方jar包,所以就自己写了一个简洁的转换工具类。
- import java.sql.Timestamp;
- import java.text.DateFormat;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Locale;
- /**
- * 日期时间转换 说明:
- * 1、java.sql.Timestamp 对应 masql下的 datetime
- * 2、java.sql.Date 是 java.util.Date 的 子类
- * 3、格式可以自定义 yyyy-MM-dd HH:mm:ss.s
- * @author john
- *
- */
- public final class DateUtils {
- // 获取java.util.Date
- // 只能参考使用,项目中禁止调用
- public final static Date getDate() {
- Date date = new Date();
- return date;
- }
- // 文本格式(String) ==》 Timestamp
- // 2010-05-31 10:45:23.1
- public final static Timestamp stringToTimestamp(String dateString)
- throws java.text.ParseException {
- DateFormat dateFormat;
- dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINESE);// 设定格式
- dateFormat.setLenient(false);// 严格控制输入 比如2010-02-31,根本没有这一天 ,也会认为时间格式不对。
- Date timeDate = dateFormat.parse(dateString);// util类型
- Timestamp dateTime = new Timestamp(timeDate.getTime());// Timestamp类型,timeDate.getTime()返回一个long型
- return dateTime;
- }
- // 获取系统 日期+时间 的毫秒数
- // 获取从1970年1月1日开始经历的毫秒数
- public final static long getDateLong() {
- Date date = new Date();
- return date.getTime();
- }
- // java.sql.Date ==》 java.util.Date
- // 子类 转 父类 不容易
- // 2013-12-26
- public final static java.util.Date sqlDateToUtilDate(java.sql.Date date) {
- String sqlDateToUtilDateString = date+"";
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
- "yyyy-MM-dd");
- java.util.Date utilDate = null;
- // 文本格式String 转为 Date
- try {
- utilDate = simpleDateFormat.parse(sqlDateToUtilDateString);
- } catch (ParseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return utilDate;
- }
- // java.util.Date ==》 java.sql.Date
- // 父类 转 子类 容易
- // 2013-12-26
- public final static java.sql.Date utilDateToSqlDate(java.util.Date date) {
- return new java.sql.Date(date.getTime());
- }
- //==========================================================================
- // 自定义日期格式 + java.util.Date==》文本格式(String)
- // 2013-12-26
- public final static String dateToSimpleDateFormat(Date date) {
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
- "yyyy-MM-dd");
- // 日期 转为 文本格式String
- return simpleDateFormat.format(date);
- }
- // 四个内建的日期格式 + java.util.Date==》文本格式(String)
- // 短的
- // 13-12-26 下午5:24
- public final static String dateToShortDateFormat(Date date) {
- DateFormat shortDateFormat = DateFormat.getDateTimeInstance(
- DateFormat.SHORT, DateFormat.SHORT);
- // 日期 转为 文本格式String
- return shortDateFormat.format(date);
- }
- // 四个内建的日期格式 + java.util.Date==》文本格式(String)
- // 中的
- // 2013-12-26 17:25:15
- public final static String dateToMediumDateFormat(Date date) {
- DateFormat mediumDateFormat = DateFormat.getDateTimeInstance(
- DateFormat.MEDIUM, DateFormat.MEDIUM);
- // 日期 转为 文本格式String
- return mediumDateFormat.format(date);
- }
- // 四个内建的日期格式 + java.util.Date==》文本格式(String)
- // 长的
- // 2013年12月26日 下午05时26分44秒
- public final static String dateTolongDateFormat(Date date) {
- DateFormat longDateFormat = DateFormat.getDateTimeInstance(
- DateFormat.LONG, DateFormat.LONG);
- // 日期 转为 文本格式String
- return longDateFormat.format(date);
- }
- // 四个内建的日期格式 + java.util.Date==》文本格式(String)
- // 完整的
- // 2013年12月26日 星期四 下午05时27分03秒 CST
- public final static String dateToFullDateFormat(Date date) {
- DateFormat fullDateFormat = DateFormat.getDateTimeInstance(
- DateFormat.FULL, DateFormat.FULL);
- // 日期 转为 文本格式String
- return fullDateFormat.format(date);
- }
- //==========================================================================
- // 自定义日期格式 + 文本格式(String)==》java.util.Date
- // 2013-12-26
- public final static Date simpleDateFormatStringToDate(String simpleDateFormatString) {
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
- "yyyy-MM-dd");
- Date date = null;
- // 文本格式String 转为 Date
- try {
- date = simpleDateFormat.parse(simpleDateFormatString);
- } catch (ParseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return date;
- }
- // 四个内建的日期格式 + 文本格式(String)==》java.util.Date
- // 短的
- // 13-12-26 下午5:24
- public final static Date shortDateFormatStringToDate(String shortDateFormatString) {
- DateFormat shortDateFormat = DateFormat.getDateTimeInstance(
- DateFormat.SHORT, DateFormat.SHORT);
- Date date = null;
- // 文本格式String 转为 Date
- try {
- date = shortDateFormat.parse(shortDateFormatString);
- } catch (ParseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return date;
- }
- // 四个内建的日期格式 + 文本格式(String)==》java.util.Date
- // 中的
- // 2013-12-26 17:25:15
- public final static Date mediumDateFormatStringToDate(String mediumDateFormatString) {
- DateFormat mediumDateFormat = DateFormat.getDateTimeInstance(
- DateFormat.MEDIUM, DateFormat.MEDIUM);
- Date date = null;
- // 文本格式String 转为 Date
- try {
- date = mediumDateFormat.parse(mediumDateFormatString);
- } catch (ParseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return date;
- }
- // 四个内建的日期格式 + 文本格式(String)==》java.util.Date
- // 长的
- // 2013年12月26日 下午05时26分44秒
- public final static Date longDateFormatStringToDate(String longDateFormatString) {
- DateFormat longDateFormat = DateFormat.getDateTimeInstance(
- DateFormat.LONG, DateFormat.LONG);
- Date date = null;
- // 文本格式String 转为 Date
- try {
- date = longDateFormat.parse(longDateFormatString);
- } catch (ParseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return date;
- }
- // 四个内建的日期格式 + 文本格式(String)==》java.util.Date
- // 完整的
- // 2013年12月26日 星期四 下午05时27分03秒 CST
- public final static Date fullDateFormatStringToDate(String fullDateFormatString) {
- DateFormat fullDateFormat = DateFormat.getDateTimeInstance(
- DateFormat.FULL, DateFormat.FULL);
- Date date = null;
- // 文本格式String 转为 Date
- try {
- date = fullDateFormat.parse(fullDateFormatString);
- } catch (ParseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return date;
- }
- }