java时间戳与⽇期相互转换⼯具详解本⽂为⼤家分享了java⽇期与时间戳相互转换⼤全,供⼤家参考,具体内容如下
util;
import java.math.BigDecimal;
DecimalFormat;
ParseException;
SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* @author DingJiaCheng
* */
public class DateFormatUtil {
/**
* 时间戳转⽇期
* @param ms
* @return
*/
public static Date transForDate(Integer ms){
if(ms==null){
ms=0;
}
long msl=(long)ms*1000;
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date temp=null;
if(ms!=null){
try {
String str=sdf.format(msl);
temp=sdf.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
}
return temp;
}
/**
* 获取晚上9点半的时间戳
*
* @return
*/
public static int getTimes(int day, int hour, int minute) {
Calendar cal = Instance();
cal.add(Calendar.DATE, day);
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.MILLISECOND, 0);
return (int) (TimeInMillis() / 1000);
}
/**
* 获取当前时间往上的整点时间
*
* @return
*/
public static int getIntegralTime() {
Calendar cal = Instance();
cal.add(Calendar.HOUR_OF_DAY, 1);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.MILLISECOND, 0);
return (int) (TimeInMillis() / 1000);
}
public static int getIntegralTimeEnd() {
Calendar cal = Instance();
cal.set(Calendar.HOUR_OF_DAY, 24);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.MILLISECOND, 0);
return (int) (TimeInMillis() / 1000);
}
/**
* 时间戳转⽇期
* @param ms
* @return
*/
public static Date transForDate3(Integer ms){
if(ms==null){
ms=0;
}
long msl=(long)ms*1000;
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date temp=null;
if(ms!=null){
try {
String str=sdf.format(msl);
temp=sdf.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
}
return temp;
}
/**
* 时间戳转⽇期
* @param ms
* @return
*/
public static Date transForDate(Long ms){
if(ms==null){
ms=(long)0;
}
long msl=(long)ms*1000;
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date temp=null;
if(ms!=null){
try {
String str=sdf.format(msl);
temp=sdf.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
}
return temp;
}
public static String transForDate1(Integer ms){
String str = "";
if(ms!=null){
long msl=(long)ms*1000;
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); if(ms!=null){
try {
str=sdf.format(msl);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return str;
}
public static String transForDate2(Integer ms){
String str = "";
if(ms!=null){
long msl=(long)ms*1000;
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
if(ms!=null){
try {
str=sdf.format(msl);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return str;
}
public static String transForDate4(Integer ms){
String str = "";
if(ms!=null){
long msl=(long)ms*1000;
SimpleDateFormat sdf=new SimpleDateFormat("yyyy.MM.dd");
if(ms!=null){
try {
str=sdf.format(msl);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return str;
}
public static String transForDate5(Integer ms){
bigdecimal转换为integerString str = "";
if(ms!=null){
long msl=(long)ms*1000;
SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
if(ms!=null){
try {
str=sdf.format(msl);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return str;
}
public static String transForDateInChinese(Integer ms){
String str = "";
if(ms!=null){
long msl=(long)ms*1000;
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM⽉dd⽇ HH:mm:ss"); if(ms!=null){
try {
str=sdf.format(msl);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return str;
}
/**
* ⽇期转时间戳
* @param date
* @return
*/
public static Integer transForMilliSecond(Date date){
if(date==null) return null;
return (int)(Time()/1000);
}
/**
* 获取当前时间戳
* @return
*/
public static Integer currentTimeStamp(){
return (int)(System.currentTimeMillis()/1000);
/**
* ⽇期字符串转时间戳
* @param dateStr
* @return
*/
public static Integer transForMilliSecond(String dateStr){
Date date = DateFormatUtil.formatDate(dateStr);
return date == null ? null : ansForMilliSecond(date);
}
/
**
* ⽇期字符串转时间戳
* @param dateStr
* @return
*/
public static Integer transForMilliSecond(String dateStr,String format){
Date date = DateFormatUtil.formatDate(dateStr,format);
return date == null ? null : ansForMilliSecond(date);
}
/**
* ⽇期字符串转时间戳
* @param dateStr
* @param 格式如"yyyy-mm-dd"
* @return
*/
public static Integer transForMilliSecondByTim(String dateStr,String tim){ SimpleDateFormat sdf=new SimpleDateFormat(tim);
Date date =null;
try {
date = sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return date == null ? null : ansForMilliSecond(date);
}
/**
* 字符串转⽇期,格式为:"yyyy-MM-dd HH:mm:ss"
* @param dateStr
* @return
*/
public static Date formatDate(String dateStr){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date result=null;
try {
result = sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return result;
}
/**
* 字符串转⽇期,格式为:"yyyy-MM-dd HH:mm:ss"
* @param dateStr
* @return
*/
public static Date formatDate(String dateStr,String format){
SimpleDateFormat sdf=new SimpleDateFormat(format);
Date result=null;
try {
result = sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return result;
}
/**
* ⽇期转字符串
* @param date
* @return
*/
public static String formatDate(Date date){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String result=null;
result = sdf.format(date);
return result;
* ⽇期转字符串
* @param date
* @return
*/
public static String formatDate(Date date,String format){
SimpleDateFormat sdf=new SimpleDateFormat(format);
String result=null;
result = sdf.format(date);
return result;
}
/**
* 时间戳格式化输出(httl模版⽤)
*
* @param ms 时间戳
* @param format 格式化
* @return
*/
public static String transForDate(Integer ms, String format){ String str = "";
if(ms!=null){
long msl=(long)ms*1000;
SimpleDateFormat sdf=new SimpleDateFormat(format); if(!ms.equals(0)){
try {
str=sdf.format(msl);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return str;
}
/**
* 取BigDecimal类型数的整数或⼩数部分(httl模版⽤)
*
* @param b 值
* @param mode 模式 0取整 1去⼩数部分
* @return
*/
public static String splitBigDecimal(BigDecimal b, int mode) { DecimalFormat df = new DecimalFormat("0.00");
String s = df.format(b);
if(mode==0){
return s.split("\\.")[0];
}else {
return "."+s.split("\\.")[1];
}
}
/**
* 计算两个⽇期之间差的天数(httl模版⽤)
*
* @param ts1 时间戳1
* @param ts2 时间戳2
* @return
*/
public static int caculate2Days(Integer ts1, Integer ts2) {
Date firstDate = ansForDate(ts1);
Date secondDate = ansForDate(ts2);
Calendar calendar = Instance();
calendar.setTime(firstDate);
int dayNum1 = (Calendar.DAY_OF_YEAR);
calendar.setTime(secondDate);
int dayNum2 = (Calendar.DAY_OF_YEAR);
return Math.abs(dayNum1 - dayNum2);
}
/**
* 给⼿机加密中间四位加星号
*
* @param mobile
* @return
*/
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论