目录
获取当前时间
Date类型
Date date = new Date();
毫秒数
new Date().getTime()
或者
System.currentTimeMillis()
TimeStamp
Timestamp ts = new Timestamp(System.currentTimeMillis());
时间类型相互转化
SimpleDateFormat的参数
String与Date
date转为String
Date date = new Date();
SimpleDateFormat format =new SimpleDateFormat("yyyyMMddHHmmss");
String time=format.format(date);
毫秒是SSS
String转为Date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
String dateString = "2017-12-20 14:02:08";
Date dateParse = sdf.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
Date与毫秒数
Date转为毫秒
Date date = sdf.parse("2016/11/18 10:31:37 AM"); //String-->Date
Long dateLong = date.getTime(); //Date-->Long
毫秒转为Date
Long long = 1654651313;
Date date = java.sql.Date.valueOf(long); //Long-->Date
Date date = new Date(long); //Long-->Date(常用)
String与TimeStamp
String转为TimeStamp
String tsStr = "2011-05-09 11:49:45";
Timestamp ts = Timestamp.valueOf(tsStr);
注:String的类型必须形如:yyyy-mm-dd hh:mm:ss[.f...] 这样的格式,中括号表示可选,否则报错!!!
TimeStamp转为String
Timestamp ts = new Timestamp(System.currentTimeMillis());
DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String tsStr = sdf.format(ts);
Date与TimeStamp
Date转为TimeStamp
Timestamp ts = new Timestamp(date.getTime());
TimeStamp转为Date
Timestamp ts = new Timestamp(System.currentTimeMillis());
Date date = new Date();
date = ts;
但是此刻date对象指向的实体却是一个Timestamp,即date拥有Date类的方法,但被覆盖的方法的执行实体在Timestamp中。
Date加减时间
基本方法就是date转为毫秒,毫秒+-对应时间的毫秒数,再转为date
Date date = new Date();
long min30 = 30*60*1000;//30分钟
date = new Date(date .getTime() + min30);//30分钟后的时间
long hour2 = 2*60*60*1000;//2小时
date = new Date(date .getTime() + hour2 );//2小时后的时间
long day5 =5*24*60*60*1000;//5天
date = new Date(date .getTime() + day5 );//5天后的时间
Date比较时间
compareTo()
String beginTime = "2018-07-28 14:42:32";
String endTime = "2018-07-29 12:26:32";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date1 = format.parse(beginTime);
Date date2 = format.parse(endTime);
int compareTo = date1.compareTo(date2);
System.out.println(compareTo);
} catch (ParseException e) {
e.printStackTrace();
}
compareTo()方法的返回值,date1小于date2返回-1,date1大于date2返回1,相等返回0
before()或者after()
String beginTime = "2018-07-28 14:42:32";
String endTime = "2018-07-29 12:26:32";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date1 = format.parse(beginTime);
Date date2 = format.parse(endTime);
boolean before = date1.before(date2);
System.out.println(before);
} catch (ParseException e) {
e.printStackTrace();
}
before()或者after()方法的返回值为boolean类型
getTime()
String beginTime = "2018-07-28 14:42:32";
String endTime = "2018-07-29 12:26:32";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date1 = format.parse(beginTime);
Date date2 = format.parse(endTime);
long beginMillisecond = date1.getTime();
long endMillisecond = date2.getTime();
System.out.println(beginMillisecond > endMillisecond);
} catch (ParseException e) {
e.printStackTrace();
}
获取某天对应的月/周的第一天/最后一天
public static void main(String[] args) {
try {
getFirstAndLastOfMonth("20200520235959", "yyyyMMddHHmmss", "yyyyMMdd");
getFirstAndLastOfWeek("20200604235959", "yyyyMMddHHmmss", "yyyyMMdd");
} catch (Exception e) {
}
}
/**
* 获取指定日期所在周的第一天和最后一天,用下划线连接
* @param dataStr
* @return
* @throws ParseException
*/
public static String getFirstAndLastOfMonth(String dataStr,String dateFormat,String resultDateFormat) throws ParseException {
//获取当前月第一天:
Calendar c = Calendar.getInstance();
c.setTime(new SimpleDateFormat(dateFormat).parse(dataStr));
c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
String first = new SimpleDateFormat(resultDateFormat).format(c.getTime());
System.out.println("===============first:"+first);
//获取当前月最后一天
Calendar ca = Calendar.getInstance();
ca.setTime(new SimpleDateFormat(dateFormat).parse(dataStr));
ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
String last = new SimpleDateFormat(resultDateFormat).format(ca.getTime());
System.out.println("===============last:"+last);
return first+"_"+last;
}
/**
* 每周的第一天和最后一天
* @param dataStr
* @param dateFormat
* @param resultDateFormat
* @return
* @throws ParseException
*/
public static String getFirstAndLastOfWeek(String dataStr,String dateFormat,String resultDateFormat) throws ParseException {
Calendar cal = Calendar.getInstance();
cal.setTime(new SimpleDateFormat(dateFormat).parse(dataStr));
int d = 0;
if (cal.get(Calendar.DAY_OF_WEEK) == 1) {
d = -6;
} else {
d = 2 - cal.get(Calendar.DAY_OF_WEEK);
}
cal.add(Calendar.DAY_OF_WEEK, d);
// 所在周开始日期
String data1 = new SimpleDateFormat(resultDateFormat).format(cal.getTime());
System.out.println("===============data1:"+data1);
cal.add(Calendar.DAY_OF_WEEK, 6);
// 所在周结束日期
String data2 = new SimpleDateFormat(resultDateFormat).format(cal.getTime());
System.out.println("===============data2:"+data2);
return data1 + "_" + data2;
}