【时间工具】整理下java时间换算专题
首先总结了一下日期转换基础,最常用的两个工具类Date与calender,转换方法如下:
package com.zzt.spider;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 时间工具总结
* @author ZX
* @date 2018年4月20日
*
*/
public class TestTime {
public static void main(String[] args) {
//date 与calandar
try{
//首先是Date的格式化与反格式化。yyyy-MM-dd HH:mm:ss 分别对应 年-月-日 时:分:秒
//"HH:mm:ss"是24小时制的,"hh:mm:ss"是12小时制。
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//日期的格式化转换
Date now = new Date();
String format = sdf.format(now);
System.out.println(format);//结果:2018-05-02 16:51:13
//
String time = "2018-05-02 16:51:13";
Date parse = sdf.parse(time);
System.out.println(format);//
//获取时间戳
Date date = new Date();
long time1 = date.getTime();//获取时间戳方法
//给日历设置date日期:
Date d= new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(d);
//给日历设置自定义日期:
Calendar calendar2 = Calendar.getInstance();
calendar2.set(Calendar.YEAR,2018);
calendar2.set(Calendar.MONTH,11);//月份是从0开始的,所以11代表十二月
calendar2.set(Calendar.DAY_OF_MONTH,20);//
//日历转date
Date calendar2Date = calendar2.getTime();
String calendar2String = sdf.format(calendar2Date);
System.out.println(">>>>>>>>>>"+calendar2String);//结果>>>>>>>>>>2018-12-20 17:20:12
//间隔日期:
Date date1 = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date1);
cal.add(Calendar.DATE,1);
Date date2 = cal.getTime();
long leftTime = date2.getTime()-date1.getTime();
System.out.println("时间戳间隔"+leftTime);
//将时间戳间隔转换成时分秒,正在写。。
Long hh=leftTime/(1000*60*60);
Long mm=leftTime%(1000*60*60);
String betueenTime=hh+"小时"+mm+"分";
//倒计时:
}catch (Exception e){
}
}
}
工具类:计算间隔时分秒:
package com.zzt.spider;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 时间工具总结
* @author ZX
* @date 2018年4月20日
*
*/
public class TestTime {
public static void main(String[] args) throws ParseException {
//测试
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String timeStr1="2018-04-01 12:30:10";
String timeStr2="2018-04-02 13:31:20";
Date date1 = sdf.parse(timeStr1);
Date date2 = sdf.parse(timeStr2);
String dateBetween = dateBetween(date1, date2);
System.out.println(dateBetween);
}
/**
*
* @param startDate 开始date
* @param endDate 结束date
* @return
*/
public static String dateBetween(Date startDate, Date endDate) {
java.util.Calendar calStart = java.util.Calendar.getInstance();
java.util.Calendar calEnd = java.util.Calendar.getInstance();
calStart.setTime(startDate);
calEnd.setTime(endDate);
long diffMill = calEnd.getTime().getTime() - calStart.getTime().getTime();
System.out.println("间隔的毫秒数:"+diffMill);
long rtn = 0;
return formatTime(diffMill);
}
/*
* 毫秒转化时分秒毫秒
*/
public static String formatTime(Long ms) {
Integer ss = 1000;
Integer mi = ss * 60;
Integer hh = mi * 60;
Integer dd = hh * 24;
Long day = ms / dd;
Long hour = (ms - day * dd) / hh;
Long minute = (ms - day * dd - hour * hh) / mi;
Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;
StringBuffer sb = new StringBuffer();
if(day > 0) {
sb.append(day+"天");
}
if(hour > 0) {
sb.append(hour+"小时");
}
if(minute > 0) {
sb.append(minute+"分");
}
if(second > 0) {
sb.append(second+"秒");
}
if(milliSecond > 0) {
sb.append(milliSecond+"毫秒");
}
return sb.toString();
}
public static Long dateDiff(Date sDate, Date eDate, int diffType) {
java.util.Calendar calst = java.util.Calendar.getInstance();
java.util.Calendar caled = java.util.Calendar.getInstance();
calst.setTime(sDate);
caled.setTime(eDate);
long diffMill = caled.getTime().getTime() - calst.getTime().getTime();
System.out.println("diffMill"+diffMill);
long rtn = 0;
switch (diffType) {
case Calendar.MILLISECOND:
rtn = diffMill;
break;
case Calendar.SECOND:
rtn = diffMill / 1000;
break;
case Calendar.MINUTE:
rtn = diffMill / 1000 / 60;
break;
case Calendar.HOUR:
rtn = diffMill / 1000 / 3600;
break;
case Calendar.DATE:
rtn = diffMill / 1000 / 60 / 60 / 24;
break;
case Calendar.MONTH:
rtn = diffMill / 1000 / 60 / 60 / 24 / 12;
break;
case Calendar.YEAR:
rtn = diffMill / 1000 / 60 / 60 / 24 / 365;
break;
}
return rtn;
}
}
获取某月第一天与最后一天:
/**
* 获得该月第一天
* @param year
* @param month
* @return
*/
public static String getFirstDayOfMonth(int year,int month){
Calendar cal = Calendar.getInstance();
//设置年份
cal.set(Calendar.YEAR,year);
//设置月份
cal.set(Calendar.MONTH, month-1);
//获取某月最小天数
int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
//设置日历中月份的最小天数
cal.set(Calendar.DAY_OF_MONTH, firstDay);
//格式化日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String firstDayOfMonth = sdf.format(cal.getTime());
return firstDayOfMonth;
}
/**
* 获取某年某月的最后一天
* @param year
* @param month
* @return
*/
public static String getLatDayOfMonth(int year ,int month) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
//设置年份
/* cal.set(Calendar.YEAR,year);
//设置月份
cal.set(Calendar.MONTH, month-1);*/
//获取某月最大天数
// int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
//设置日历中月份的最大天数
// cal.set(Calendar.DAY_OF_MONTH, lastDay);
//格式化日期
cal.set(year, month-1, 1);
cal.roll(Calendar.DATE, -1);
String lastDayOfMonth = sdf.format(cal.getTime());
return lastDayOfMonth;
}