package utils;
import org.apache.commons.lang.time.DateUtils;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* Created by lightClouds917
* Date 2018/1/9
* Description:date util
*/
public class MyDateUtil {
/**test those methods*/
public static void main(String[] args) throws Exception{
Date date = int2Date(42237);
//Fri Aug 21 00:00:00 CST 2015
System.out.println(int2Date(42237));
//2015.8.21
System.out.println(getYearOrMonthOrDay(date,"year"));
//2015
System.out.println(getYearOrMonthOrDay(date,"month"));
//8
System.out.println(getYearOrMonthOrDay(date,"day"));
//21
Date dateByString1 = string2Date("2017.7.1");
System.out.println(dateByString1.toString());
//Sat Jul 01 00:00:00 CST 2017
System.out.println(date2String1(dateByString1));
//2017年7月1日
System.out.println(date2String2(dateByString1));
//2017-7-1
System.out.println(string2Date("2017-8-15"));
//Tue Aug 15 00:00:00 CST 2017
System.out.println(string2Date("2017年8月15日"));
//Tue Aug 15 00:00:00 CST 2017
System.out.println(getNowYear());
//2018
System.out.println(getNowMonth());
//3
}
/**
* int类型转为日期类
* 一般用于解析Excel
*
* parse date by int
* we use it always in parsing Excel
* @param dateNum
* @return
*/
public static Date int2Date(Integer dateNum){
Calendar calendar = new GregorianCalendar(1900,0,-1);
Date dateRe = DateUtils.addDays(calendar.getTime(),dateNum);
return dateRe;
}
/**
* get year or month or day from date which instance of Date
* @param date
* @param type
* @return
*/
public static Integer getYearOrMonthOrDay(Date date,String type){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
if("year".equals(type)){
return calendar.get(Calendar.YEAR);
}else if("month".equals(type)){
return calendar.get(Calendar.MONTH)+1;
}else if("day".equals(type)){
return calendar.get(Calendar.DAY_OF_MONTH);
}
return null;
}
/**
* parse String to Date
* @param str
* @return
* @throws Exception
*/
public static Date string2Date(String str) throws Exception{
Date date = null;
if(str.contains("/")){
str = str.replace("/","-");
}else if(str.contains(".")){
str = str.replace(".","-");
}else if(!str.contains("日") && str.contains("年") && str.contains("月")){
str = str.replace("年","-");
str = str.replace("月","");
}else if(str.contains("日") && str.contains("年") && str.contains("月")){
str = str.replace("年","-");
str = str.replace("月","-");
str = str.replace("日","");
}
try {
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
date = format1.parse(str);
}catch (Exception ex){
SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM");
date = format2.parse(str);
}
return date;
}
/**
* parse Date to String eg:2017年7月1日
* @param date
* @return
*/
public static String date2String1(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
String str = calendar.get(Calendar.YEAR)+"年"+(calendar.get(Calendar.MONTH)+1)+"月"+calendar.get(Calendar.DAY_OF_MONTH)+"日";
return str;
}
/**
* parse Date to String eg:2017-7-1
* @param date
* @return
*/
public static String date2String2(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
String str = calendar.get(Calendar.YEAR)+"-"+(calendar.get(Calendar.MONTH)+1)+"-"+calendar.get(Calendar.DAY_OF_MONTH);
return str;
}
/**
* get now year eg:2018
*/
public static Integer getNowYear(){
Calendar cal = Calendar.getInstance();
int nowYear = cal.get(Calendar.YEAR);
return nowYear;
}
/**
* get now month eg:8
*/
public static Integer getNowMonth(){
Calendar cal = Calendar.getInstance();
int nowMonth = cal.get(Calendar.MONTH) + 1;
return nowMonth;
}
}