前言:
对于新手程序员的我来说,写业务代码是现在的日常,在此过程中经常需要对日期时间进行处理,我挑了几个较有用的日期处理函数分享给大家。
正文:
1、将某格式的时间字符串转化成毫秒时间戳表示的字符串:
public static String dateTimeStrToMills(String dateTime,String format){
String dateStr = null;
SimpleDateFormat sdf = new SimpleDateFormat(format);
Calendar calendar = Calendar.getInstance();
calendar.clear();
try {
Date d = new Date();
d = sdf.parse(dateTime);
calendar.setTime(d);
dateStr = calendar.getTimeInMillis()+"ms";
} catch (ParseException e) {
e.printStackTrace();
}
return dateStr;
}
测试例:
@Test
public void test1(){
String date = "2018-10-10 08:30:00";
String format = "yyyy-MM-dd HH:mm:ss";
String timeMS = DateTimeUtils.dateTimeStrToMills(date,format);
System.out.println("毫秒时间:" + timeMS);//毫秒时间:1539131400000ms
}
2、获取某个月的天数:
public static int getDayNumOfMonth(int year,int month){
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(year, month,0);
return calendar.get(Calendar.DAY_OF_MONTH);
}
测试例:
@Test
public void test2(){
int year = 2018;
int month = 10;
int dayNum = DateTimeUtils.getDayNumOfMonth(year,month);
System.out.println("dayNum:" + dayNum);//dayNum:31
}
3、获取某日、月、年前后的日期:
public static String getBeforeOrAfterDateType(int num,String date,String format,int timeType){
SimpleDateFormat sdf = new SimpleDateFormat(format);
String resultDate = "";
Calendar calendar = Calendar.getInstance();
calendar.clear();
try {
Date d = new Date();
d = sdf.parse(date);
calendar.setTime(d);
calendar.add(timeType, num);//一天的结束是第二天的开始
resultDate = sdf.format(calendar.getTime());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return resultDate;
}
测试例:
@Test
public void test3(){
//获取某个月前面一个月
int num = -1;
String date = "2018-10";
String format = "yyyy-MM";
int timeType = Calendar.MONTH;
String resultDate = DateTimeUtils.getBeforeOrAfterDateType(num,date,format,timeType);
System.out.println("resultDate:" + resultDate);//resultDate:2018-09
//获取某天几天后的日期
num = 3;
date = "2018-10-05";
format = "yyyy-MM-dd";
timeType = Calendar.DAY_OF_MONTH;
resultDate = DateTimeUtils.getBeforeOrAfterDateType(num,date,format,timeType);
System.out.println("resultDate:" + resultDate);//resultDate:2018-10-08
}
4、根据毫秒时间戳获得格式化后的日期:
public static String millisecondToDate(Long millisecond,String dateFormat){
Date date = new Date(millisecond);
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
SimpleDateFormat format = new SimpleDateFormat(dateFormat);
String sb = format.format(gc.getTime());
return sb;
}
测试例:
@Test
public void test4(){
long mill = 1539131400000l;
String format = "yyyy-MM-dd HH:mm:ss";
String date = DateTimeUtils.millisecondToDate(mill,format);
System.out.println("date:" + date);//date:2018-10-10 08:30:00
}
5、获取某月第一天:
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;
}
测试例:
@Test
public void test5(){
int year = 2018;
int month = 11;
String format = "yyyy-MM-dd";
String date = DateTimeUtils.getFirstDayOfMonth(year,month,format);
System.out.println("date:" + date);//date:2018-11-01
}
6、获取某月最后一天:
public static String getLastDayOfMonth(int year,int month,String format){
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month-1);
int lastDay = 0;
lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_MONTH, lastDay);
SimpleDateFormat sdf = new SimpleDateFormat(format);
String lastDayOfMonth = sdf.format(cal.getTime());
return lastDayOfMonth;
}
测试例:
@Test
public void test6(){
int year = 2018;
int month = 11;
String format = "yyyy-MM-dd";
String date = DateTimeUtils.getLastDayOfMonth(year,month,format);
System.out.println("date:" + date);//date:2018-11-30
}
7、获取起止日期之间的所有字符串(可自定义间隔、格式、日期类型):
public static List getDatesBetweenTwoDate(String begin,String end,int num,int timeType,String format){
List lDates;
lDates = new ArrayList<>();
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date beginDate = sdf.parse(begin);
Date endDate = sdf.parse(end);
lDates.add(sdf.format(beginDate));
Calendar calendar = Calendar.getInstance();
calendar.setTime(beginDate);
boolean bContinue = true;
while(bContinue){
calendar.add(timeType, num);
if(endDate.after(calendar.getTime())){
lDates.add(sdf.format(calendar.getTime()));
}else{
break;
}
}
lDates.add(sdf.format(endDate));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return lDates;
}
测试例:
@Test
public void test7(){
String startDate = "2018-10-10";
String endDate = "2018-10-15";
String format = "yyyy-MM-dd";
int num = 1;
int timeType = Calendar.DAY_OF_MONTH;
List timeList = DateTimeUtils.getDatesBetweenTwoDate(startDate,endDate,num,timeType,format);
System.out.println(timeList.toString());
//[2018-10-10, 2018-10-11, 2018-10-12, 2018-10-13, 2018-10-14, 2018-10-15]
}