java 日期各种格式之间的相互转换实例代码
更新时间:2017年02月21日 09:49:32 投稿:lqh
这篇文章主要介绍了java 日期各种格式之间的相互转换实例代码的相关资料,需要的朋友可以参考下
java 日期各种格式之间的相互转换实例代码
java日期各种格式之间的相互转换,直接调用静态方法
实例代码:
java日期各种格式之间的相互转换,直接调用静态方法
package com.hxhk.cc.util;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.lowagie.text.pdf.codec.postscript.ParseException;
public class DateUtil {
/**
* @param args
* @throws java.text.ParseException
* @throws ParseException
*/
public static void main(String[] args) throws ParseException, java.text.ParseException {
DateUtil du = new DateUtil();
//String s = du.numToDate(1350144260, "yyyy-MM-dd hh:mm:ss");
long time = du.stringToLong("2012-10-15 8:44:53", "yyyy-MM-dd hh:mm:ss")/1000;
long time1 = du.stringToLong("2012-10-15 20:44:53", "yyyy-MM-dd hh:mm:ss")/1000;
String date = du.longToString(1350470693,"yyyy-MM-dd hh:mm:ss" );
System.out.println(time);
System.out.println(time1);
System.out.println(date);
}
// date类型转换为String类型
// formatType格式为yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
// data Date类型的时间
public static String dateToString(Date data, String formatType) {
return new SimpleDateFormat(formatType).format(data);
}
// long类型转换为String类型
// currentTime要转换的long类型的时间
// formatType要转换的string类型的时间格式
public static String longToString(long currentTime, String formatType)
throws ParseException, java.text.ParseException {
Date date = longToDate(currentTime, formatType); // long类型转成Date类型
String strTime = dateToString(date, formatType); // date类型转成String
return strTime;
}
// string类型转换为date类型
// strTime要转换的string类型的时间,formatType要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
// HH时mm分ss秒,
// strTime的时间格式必须要与formatType的时间格式相同
public static Date stringToDate(String strTime, String formatType)
throws ParseException, java.text.ParseException {
SimpleDateFormat formatter = new SimpleDateFormat(formatType);
Date date = null;
date = formatter.parse(strTime);
return date;
}
// long转换为Date类型
// currentTime要转换的long类型的时间
// formatType要转换的时间格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
public static Date longToDate(long currentTime, String formatType)
throws ParseException, java.text.ParseException {
Date dateOld = new Date(currentTime); // 根据long类型的毫秒数生命一个date类型的时间
String sDateTime = dateToString(dateOld, formatType); // 把date类型的时间转换为string
Date date = stringToDate(sDateTime, formatType); // 把String类型转换为Date类型
return date;
}
// string类型转换为long类型
// strTime要转换的String类型的时间
// formatType时间格式
// strTime的时间格式和formatType的时间格式必须相同
public static long stringToLong(String strTime, String formatType)
throws ParseException, java.text.ParseException {
Date date = stringToDate(strTime, formatType); // String类型转成date类型
if (date == null) {
return 0;
} else {
long currentTime = dateToLong(date); // date类型转成long类型
return currentTime;
}
}
// date类型转换为long类型
// date要转换的date类型的时间
public static long dateToLong(Date date) {
return date.getTime();
}
public static String numToDate(int number,String formatType){
Date date = new Date(number);
SimpleDateFormat sdf = new SimpleDateFormat(formatType);
return sdf.format(date);
}
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
相关文章
新版本IntelliJ IDEA 构建maven,并用Maven创建一个web项目(图文教程)
这篇文章主要介绍了新版本IntelliJ IDEA 构建maven,并用Maven创建一个web项目的图文教程,需要的朋友可以参考下2018-01-01
Java中使用Thread类和Runnable接口实现多线程的区别
这篇文章主要介绍了使用Thread类和Runnable接口实现多线程的区别,本文给大家介绍了两种实现方式的步骤,除了以上两种多线程实现方式,还可以使用 Callable 接口实现,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下2022-07-07
Spring Integration 实现消息驱动的详细步骤
Spring Integration是一个用于构建消息驱动的中间件轻量级框架,它提供了一种模型和工具,用于在Spring应用程序中实现企业集成模式,这篇文章主要介绍了Spring Integration 实现消息驱动,需要的朋友可以参考下2024-05-05
spring cloud 使用Hystrix 实现断路器进行服务容错保护的方法
本篇文章主要介绍了spring cloud 使用Hystrix 实现断路器进行服务容错保护的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2018-05-05
POI XSSFSheet shiftRows bug问题解决
这篇文章主要介绍了POI XSSFSheet shiftRows bug问题解决,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-07-07


最新评论