以前的博客“Java中的日期常用格式化”提到的都是当前日期的格式化,这篇博客写指定日期的格式化。当然类似于前一篇博客的内容就不多写了,写一些不一样的,如一个月的总天数,两个日期之间相隔的天数,字符串类型日期的格式化等。
废话不多说,上代码:
public class TimeTest {
/**
* 获取指定日期的年份
* @throws ParseException
* **/
public static Integer year(String strDate){
int year = 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = new GregorianCalendar();
try{
Date date1 = sdf.parse(strDate);
calendar.setTime(date1); //放入日期
year = calendar.get(Calendar.YEAR);
}catch(ParseException e){
e.printStackTrace();
}
return year;
}
/**
* 获取指定日期的月份
* @throws ParseException
* **/
public static Integer month(String strDate){
int month = 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = new GregorianCalendar();
try{
Date date1 = sdf.parse(strDate);
calendar.setTime(date1); //放入日期
month = calendar.get(Calendar.MONTH)+1;
}catch(ParseException e){
e.printStackTrace();
}
return month;
}
/**
* 获取指定日期的天
* @throws ParseException
* **/
public static Integer day(String strDate){
int day = 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = new GregorianCalendar();
try{
Date date1 = sdf.parse(strDate);
calendar.setTime(date1); //放入日期
day = calendar.get(Calendar.DAY_OF_MONTH);
}catch(ParseException e){
e.printStackTrace();
}
return day;
}
/**
* 获取指定日期所在月份的天数
* @throws ParseException
* **/
public static Integer getActualMaximum(String strDate){
int max = 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
Calendar calendar = new GregorianCalendar();
try{
Date date1 = sdf.parse(strDate);
calendar.setTime(date1); //放入日期
max = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
}catch(ParseException e){
e.printStackTrace();
}
return max;
}
/**
* 将字符串类型的日期进行格式化,返回格式化之后字符串类型的日期
* @param String strDate:字符串格式的日期
* @return 字符串类型格式化后的日期,如:2016.06.05
* **/
public static String formatStringDateToYYYYMMDD(String strDate){
if (strDate == null)
return "";
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");// 实例化模板对象
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy.MM.dd");// 实例化模板对象
Date d = null ;
try{
d = sdf1.parse(strDate) ; // 将给定的字符串中的日期提取出来
}catch(Exception e){ // 如果提供的字符串格式有错误,则进行异常处理
e.printStackTrace() ;// 打印异常信息
e.printStackTrace();
}
return sdf2.format(d);//按照规定格式进行格式化
}
/**
* 返回两个日期之间相差的天数,当date2>=date1时返回正数,否则返回负数
* @param date1:String类型 时间1
* @param date2:String类型 时间2
* @return days:返回date1和date2之间相差的天数
* */
public static int dateDiff(String date1, String date2){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar1 = new GregorianCalendar();
Calendar calendar2 = new GregorianCalendar();
try{
calendar1.setTime(sdf.parse(date1));
calendar2.setTime(sdf.parse(date2));
}catch(ParseException e){
e.printStackTrace();
}
//设置时间为0时
calendar1.set(Calendar.HOUR_OF_DAY, 0);
calendar1.set(Calendar.MINUTE, 0);
calendar1.set(Calendar.SECOND, 0);
calendar2.set(Calendar.HOUR_OF_DAY, 0);
calendar2.set(Calendar.MINUTE, 0);
calendar2.set(Calendar.SECOND, 0);
//得到两个日期相差的天数
int days = ((int)(calendar2.getTime().getTime()/1000)-(int)(calendar1.getTime().getTime()/1000))/3600/24;
return days;
}
/**
* 返回指定年份月份的最后一天的时间
* @param strDate:要返回该月最后一天的字符串日期,必须包括年月
* */
public static String getLastDayEndForMonth(String strDate){
String time="";
int lastDay = getActualMaximum(strDate);//获取日期的所在月份的天数
int year = year(strDate);//获取年份
int month = month(strDate);//获取月份
time = year+"-"+month+"-"+lastDay+" 23:59:59";
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try{
date = df.parse(time);
}catch(ParseException e){
e.printStackTrace();
}
return df.format(date);
}
/**
* 返回指定年份月份的第一天的时间
* @param strDate:要返回该月最后一天的字符串日期,必须包括年月
* */
public static String getFirstDayEndForMonth(String strDate){
String time="";
int year = year(strDate);//获取年份
int month = month(strDate);//获取月份
time = year+"-"+month+"-01 00:00:00.0";
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try{
date = df.parse(time);
}catch(ParseException e){
e.printStackTrace();
}
return df.format(date);
}
public static void main(String[] args) throws ParseException {
System.out.println("年份:"+year("2016-06-16 00:00:00.000"));
System.out.println("月份:"+month("2016-06-16 00:00:00.000"));
System.out.println("天:"+day("2016-06-16 00:00:00.000"));
System.out.println("6月的总天数:"+getActualMaximum("2016-06-16 00:00:00.000"));
System.out.println("字符串类型的日期格式化:"+formatStringDateToYYYYMMDD("2016-06-16 00:00:00.000"));
System.out.println("获取两个日期之间相差的天数:"+dateDiff("2016-06-16 00:00:00.000","2015-03-21 00:00:00.000"));
System.out.println("指定年份月份的最后一天的时间:"+getLastDayEndForMonth("2016-06-16 00:00:00.000"));
System.out.println("指定年份月份的第一天的时间:"+getFirstDayEndForMonth("2016-03-02 00:00:00.000"));
}
}