本篇主要记录java在日期时间上的处理,OK,话不多说,开码!
jdk1.8之前的时间日期处理基本都是通过以下两个类来完成
java.util.Calendar
java.text.SimpleDateFormat
通过下面这个案例来了解一下基本的用法
package com.chenfu;
import org.junit.Test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* @Author: romantic_ke@163.com
* @Description:
* @Date: 2019/1/29 13:17
*/
public class CalendarTest {
private Long ONE_WEEK_TIME = 3600 * 24 * 7L;
/**
* 获取年月日时分秒
*/
@Test
public void test01(){
Calendar instance = Calendar.getInstance();
System.out.println("YEAR:" + instance.get(Calendar.YEAR));
// 从0开始的 0-11
System.out.println("MONTH:" + instance.get(Calendar.MONTH));
// 这一天是这个月的第几天
System.out.println("DAY_OF_MONTH:" + instance.get(Calendar.DAY_OF_MONTH));
// 这一天是这一年的第几天
System.out.println("DAY_OF_YEAR:" + instance.get(Calendar.DAY_OF_YEAR));
// 这一天是一周的第几天 注意,外国是以周日为一周的开始的
System.out.println("DAY_OF_WEEK:" + instance.get(Calendar.DAY_OF_WEEK));
// 修改为周一为一周的开始
instance.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
System.out.println("DAY_OF_WEEK2:" + instance.get(Calendar.DAY_OF_WEEK));
// 这一天所在的周在月份是第几周
System.out.println("DAY_OF_WEEK_IN_MONTH:" + instance.get(Calendar.DAY_OF_WEEK_IN_MONTH));
// 12小时制
System.out.println("HOUR:" + instance.get(Calendar.HOUR));
// 24小时制
System.out.println("HOUR_OF_DAY:" + instance.get(Calendar.HOUR_OF_DAY));
System.out.println("MINUTE:" + instance.get(Calendar.MINUTE));
System.out.println("SECOND:" + instance.get(Calendar.SECOND));
}
/**
* 传递进来一个日期,只要这一周的固定一天
* 比如:无论传进来哪一天,我只要返回上一周的周末
*/
@Test
public void test02(){
Calendar calendar = Calendar.getInstance();
// 设置一周的第一天从那一天开始
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
calendar.set(Calendar.HOUR_OF_DAY,0);
calendar.set(Calendar.MINUTE,0);
calendar.set(Calendar.SECOND,0);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long millis = calendar.getTimeInMillis();
String s = simpleDateFormat.format(millis - ONE_WEEK_TIME * 1000);
System.out.println(s);
}
/**
* 获取一个月的最后一天
*/
@Test
public void test03() throws ParseException {
String date = "2019-01-18 00:00:00";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar instance = Calendar.getInstance();
Date parse = simpleDateFormat.parse(date);
instance.setTime(parse);
// 设置那一天要的就是当月的哪一天,如果是0,则是上个月的最后一天
instance.set(Calendar.DAY_OF_MONTH,-1);
instance.set(Calendar.HOUR_OF_DAY,0);
instance.set(Calendar.MINUTE,0);
instance.set(Calendar.SECOND,0);
long millis = instance.getTimeInMillis();
String s = simpleDateFormat.format(millis);
System.out.println(s);
}
/**
* 获取当前月的最后一天
*/
@Test
public void test07(){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar instance = Calendar.getInstance();
instance.set(Calendar.DAY_OF_MONTH,instance.getActualMaximum(Calendar.DAY_OF_MONTH));
instance.getTime();
String s = simpleDateFormat.format(instance.getTimeInMillis());
System.out.println(s);
}
@Test
public void test08(){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar instance = Calendar.getInstance();
instance.set(Calendar.DAY_OF_MONTH,instance.getActualMinimum(Calendar.DAY_OF_MONTH));
String format = simpleDateFormat.format(instance.getTimeInMillis());
System.out.println(format);
}
}
日期处理工具类
package com.chenfu.commonds;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* @Author: romantic_ke@163.com
* @Description:日期格式化
* @Date: 2019/1/29 14:08
*/
public class DateUtils {
private static Long ONE_WEEK_TIME = 3600 * 24 *7L;
private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
/**
* 将unix格式的时间戳转化为字符串日期
* @param time
* @return
*/
public static String formateDate(Long time){
String format = simpleDateFormat.format(time * 1000);
return format;
}
/**
* 获得日期对应的unix时间戳
* @param time
* @return
* @throws ParseException
*/
public static Long formateDate(String time) throws ParseException {
Date parse = simpleDateFormat.parse(time);
Calendar instance = Calendar.getInstance();
instance.setTime(parse);
return instance.getTimeInMillis()/1000;
}
/**
* 获取指定的日期上一周的周末
* @param time
* @return
* @throws ParseException
*/
public static Long getWeekEnd(String time) throws ParseException {
Date parse = simpleDateFormat.parse(time);
Calendar instance = Calendar.getInstance();
instance.setTime(parse);
instance.setFirstDayOfWeek(Calendar.MONDAY);
instance.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
instance.set(Calendar.HOUR_OF_DAY,0);
instance.set(Calendar.MINUTE,0);
instance.set(Calendar.SECOND,0);
return instance.getTimeInMillis()/1000 - ONE_WEEK_TIME;
}
public static Long getWeekEnd(Long time){
Calendar instance = Calendar.getInstance();
instance.setTimeInMillis(time * 1000);
instance.setFirstDayOfWeek(Calendar.MONDAY);
instance.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
instance.set(Calendar.HOUR_OF_DAY,0);
instance.set(Calendar.MINUTE,0);
instance.set(Calendar.SECOND,0);
return instance.getTimeInMillis()/1000 - ONE_WEEK_TIME;
}
/**
* 获取指定的日期的周一
* @param time
* @return
* @throws ParseException
*/
public static Long getWeekStart(String time) throws ParseException {
Date parse = simpleDateFormat.parse(time);
Calendar instance = Calendar.getInstance();
instance.setTime(parse);
instance.setFirstDayOfWeek(Calendar.MONDAY);
instance.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
instance.set(Calendar.HOUR_OF_DAY,0);
instance.set(Calendar.MINUTE,0);
instance.set(Calendar.SECOND,0);
return instance.getTimeInMillis()/1000;
}
public static Long getWeekStart(Long time){
Calendar instance = Calendar.getInstance();
instance.setTimeInMillis(time * 1000);
instance.setFirstDayOfWeek(Calendar.MONDAY);
instance.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
instance.set(Calendar.HOUR_OF_DAY,0);
instance.set(Calendar.MINUTE,0);
instance.set(Calendar.SECOND,0);
return instance.getTimeInMillis()/1000;
}
/**
* 增加或减少指定天数
* @param time
* @param days
* @return
*/
public static String addDays(String time,Integer days) throws ParseException {
Date parse = simpleDateFormat.parse(time);
Calendar instance = Calendar.getInstance();
instance.setTime(parse);
instance.add(Calendar.DATE,days);
return simpleDateFormat.format(instance.getTimeInMillis());
}
/**
* 获取当前月(传递进来的日期)的第一天
*/
public static String getMonthFirstDay(String time) throws ParseException {
Date parse = simpleDateFormat.parse(time);
Calendar instance = Calendar.getInstance();
instance.setTime(parse);
// instance.set(Calendar.DAY_OF_MONTH,1);
instance.set(Calendar.DAY_OF_MONTH,instance.getActualMinimum(Calendar.DAY_OF_MONTH));
instance.set(Calendar.HOUR_OF_DAY,0);
instance.set(Calendar.MINUTE,0);
instance.set(Calendar.SECOND,0);
return simpleDateFormat.format(instance.getTimeInMillis());
}
}
jdk8的时间日期处理API更强大和安全,也推荐看到此篇的同学都去了解掌握一下,但是毕竟现在企业上基本都还是用jdk8之前的时间处理api,所以两者要兼顾哦