/**
* @Description: 求两个时间戳的差值,转换为小时
*/
public Float getDuration(String start, String end){
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
long startMilli = LocalDateTime.parse(start, df).toInstant(ZoneOffset.of("+8")).toEpochMilli();
long endMilli = LocalDateTime.parse(end, df).toInstant(ZoneOffset.of("+8")).toEpochMilli();
// long day = 24*60*60*1000; 转换为天
long day = 60*60*1000;
return (float)(endMilli - startMilli)/day;
}
/**
* 十位时间戳 转date
* @param date
* @return
*/
private Date stringToDate10(String date){
Timestamp timestamp = new Timestamp(Long.valueOf(date) * 1000);
return timestamp;
}
/**
* 2020-01-01 格式化为date
* @return
*/
private Date stringToDate_(String date){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date strtodate = null;
try {
strtodate = sdf.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return strtodate;
}
/**
* 2020-01-01 00:00:00 格式化为date
* @return
*/
private Date stringToDateTime(String date){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date strtodate = null;
try {
strtodate = sdf.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return strtodate;
}
/**
* 20210913 格式化为date
* @return
*/
private Date stringToDate(int date){
String s = String.valueOf(date);
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
ParsePosition pos = new ParsePosition(0);
Date strtodate = format.parse(s, pos);
return strtodate;
}
/**
* 获取本月第一天 返回13位时间戳
* @return
*/
private long getMonthFirst(){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
// 本月起始
Calendar thisMonthFirstDateCal = Calendar.getInstance();
thisMonthFirstDateCal.set(Calendar.DAY_OF_MONTH, 1);
String thisMonthFirstTime = format.format(thisMonthFirstDateCal.getTime()) + " 00:00:00";
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
try{
date = sf.parse(thisMonthFirstTime);
} catch(ParseException e) {
e.printStackTrace();
}
long time = date.getTime();
return time;
}
/**
* 获取本月最后一天 返回13位时间戳
* @return
*/
private long getMonthFinal(){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
// 本月起始
Calendar thisMonthFirstDateCal = Calendar.getInstance();
thisMonthFirstDateCal.set(Calendar.DAY_OF_MONTH, thisMonthFirstDateCal.getActualMaximum(Calendar.DAY_OF_MONTH));
String thisMonthFirstTime = format.format(thisMonthFirstDateCal.getTime()) + " 00:00:00";
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
try{
date = sf.parse(thisMonthFirstTime);
} catch(ParseException e) {
e.printStackTrace();
}
long time = date.getTime();
return time;
}
/**
* 获取上月第一天 返回13位时间戳
* @return
*/
private long getLastMonthFirst(){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//获取前月的第一天
Calendar cal_1=Calendar.getInstance();//获取当前日期
cal_1.add(Calendar.MONTH, -1);
cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
String firstDay = format.format(cal_1.getTime()) + " 00:00:00";
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
try{
date = sf.parse(firstDay);
} catch(ParseException e) {
e.printStackTrace();
}
long time = date.getTime();
return time;
}
/**
* 获取上月最后一天 返回13位时间戳
* @return
*/
private long getLastMonthFinal(){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//获取前月的最后一天
Calendar cale = Calendar.getInstance();
cale.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天
String lastDay = format.format(cale.getTime()) + " 00:00:00";
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
try{
date = sf.parse(lastDay);
} catch(ParseException e) {
e.printStackTrace();
}
long time = date.getTime();
return time;
}
/**
* 获取当天时间 返回13位时间戳
* @return
*/
private long getToDay(){
Date date = new Date();
return date.getTime();
}
/**
* 获取本月第一天 返回10位时间戳
* @return
*/
private int getMonthFirst(){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar instance = Calendar.getInstance();
instance.add(Calendar.MONTH,0);
instance.set(Calendar.DAY_OF_MONTH,1);
Date time = instance.getTime();
String format1 = format.format(time);
int from = 0;
try {
from = (int)(format.parse(format1).getTime()/1000);
} catch (ParseException e) {
e.printStackTrace();
}
return from;
}
/**
* 获取本月最后一天 返回10位时间戳
* @return
*/
private int getMonthFinal(){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar ca = Calendar.getInstance();
ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
String last = format.format(ca.getTime());
int to = 0;
try {
to = (int)(format.parse(last).getTime()/1000);
} catch (ParseException e) {
e.printStackTrace();
}
return to;
}
/**
* 获取上月第一天 返回10位时间戳
* @return
*/
private int getLastMonthFirst(){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//获取前月的第一天
Calendar cal_1=Calendar.getInstance();//获取当前日期
cal_1.add(Calendar.MONTH, -1);
cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
String firstDay = format.format(cal_1.getTime());
int from = 0;
try {
from = (int)(format.parse(firstDay).getTime()/1000);
} catch (ParseException e) {
e.printStackTrace();
}
return from;
}
/**
* 获取上月最后一天 返回10位时间戳
* @return
*/
private int getLastMonthFinal(){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//获取前月的最后一天
Calendar cale = Calendar.getInstance();
cale.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天
String lastDay = format.format(cale.getTime());
int to = 0;
try {
to = (int)(format.parse(lastDay).getTime()/1000);
} catch (ParseException e) {
e.printStackTrace();
}
return to;
}
/**
* 获取当天时间 返回10位时间戳
* @return
*/
private int getToDay(){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
String time = format.format(date);
int to = 0;
try {
to = (int)(format.parse(time).getTime()/1000);
} catch (ParseException e) {
e.printStackTrace();
}
return to;
}
/**
* 获取当天时间
* @return
*/
private String getToDay(){
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
Date date = new Date();
String to = format.format(date);
return to;
}
/**
* 获取本月最后一天时间
* @return
*/
private String getMonthFinal(){
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
Calendar ca = Calendar.getInstance();
ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
String last = format.format(ca.getTime());
return last;
}
/**
* 获取上月第一天
* @return
*/
private String getLastMonthFirst(){
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
//获取前月的第一天
Calendar cal_1=Calendar.getInstance();//获取当前日期
cal_1.add(Calendar.MONTH, -1);
cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
String firstDay = format.format(cal_1.getTime());
return firstDay;
}
/**
* 获取上月最后一天
* @return
*/
private String getLastMonthFinal(){
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
//获取前月的最后一天
Calendar cale = Calendar.getInstance();
cale.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天
String lastDay = format.format(cale.getTime());
return lastDay;
}
* 13位时间戳 转date
* @param date
* @return
*/
private Date stringToDate13(Long date){
Timestamp timestamp = new Timestamp(date);
return timestamp;
}
时间转换 java
最新推荐文章于 2022-06-02 22:23:04 发布