Java 时间格式转换

时间:2019-09-23
本文章向大家介绍Java 时间格式转换,主要包括Java 时间格式转换使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。


public class DateUtil {

/**
* 生成ISO-8601 规范的时间格式
*
* @param date
* @return
*/
public static String formatiso8601Datestring(Date date) {
String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
return DateFormatUtils.format(date, pattern);
}

/***
* 生成时间戳
* @param s
* @return 时间戳
*/
public static String getTimeStamp(String s) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
Date date = null;

try {
date = simpleDateFormat.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
long ts = Objects.requireNonNull(date).getTime();
return String.valueOf(ts);
}

/***
* 返回时间戳对应的时间
* @param s
* @return 时间
*/
public static Timestamp getDateFromTimeStamp(String s) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long lt = new Long(s);
Date date1 = new Date(lt);
return Timestamp.valueOf(simpleDateFormat.format(date1));
}

* 获取反时间戳
*
* @return
*/
public static Long getCurrentReverseTime() {
long longTime = System.currentTimeMillis() * 1000000 + CalculateUtil.getNext(999999);
return Long.MAX_VALUE - longTime;
}

/**
* 获取原时间戳
*
* @param reverseTime
* @return
*/
public static Long recoverReverseTime(Long reverseTime) {
long longTime = Long.MAX_VALUE - reverseTime;
return longTime / 1000000;
}

/**
* 生成页面普通展示时间
*
* @param date
* @return
*/
public static String formatNormalDateString(Date date) {
String pattern = "yyyy-MM-dd HH:mm:ss";
return DateFormatUtils.format(date, pattern);
}

//时间戳转化成时间
public Date formatTime(Long time) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strTime = format.format(time);
Date date = format.parse(strTime);
return date;
}

public static void main(String[] args) {

System.out.println(getTimeStamp("20190603"));
}

}

原文地址:https://www.cnblogs.com/banml/p/11572550.html