java 日期工具类整理

来源:互联网 发布:linux查看log文件 编辑:程序博客网 时间:2024/05/16 06:39

package com.self;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

private static Calendar calendar = Calendar.getInstance();
private static SimpleDateFormat dateFormat ;

/**
* @Title: getFirstDayOfMonth
* @Description:获取本月的第一天
* @param1 Date date
* @param2 String pattern
* @return String
* @throws ParseException
*/
public static String getFirstDayOfMonth(Date date,String pattern){
calendar.set(Calendar.DAY_OF_MONTH, calendar .getActualMinimum(Calendar.DAY_OF_MONTH));
dateFormat = new SimpleDateFormat(pattern);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String temp = sdf.format(calendar.getTime());
Date tempDate = null;
try {
tempDate = sdf.parse(temp);
} catch (Exception e) {
e.printStackTrace();
}

return dateFormat.format(tempDate);
}


/**
* @Title: getLastDayOfMonth
* @Description:获取本月的最后一天
* @param1 Date date
* @param2 String pattern
* @return String
*/
public static String getLastDayOfMonth(Date date,String pattern) {
calendar.set(Calendar.DAY_OF_MONTH, calendar .getActualMaximum(Calendar.DAY_OF_MONTH));
dateFormat = new SimpleDateFormat(pattern);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String temp = sdf.format(calendar.getTime());
Date tempDate = null;
try {
tempDate = sdf.parse(temp);
} catch (Exception e) {
e.printStackTrace();
}

return dateFormat.format(tempDate);
}

/**
*
* @Title: getLastDayOfMonth
* @Description:获取本年的第一天
* @param1 Date date
* @param2 String pattern
* @return String
*/
public static String getFirstDayOfYear(Date date,String pattern) {
calendar.set(Calendar.DAY_OF_YEAR, calendar .getActualMinimum(Calendar.DAY_OF_YEAR));
dateFormat = new SimpleDateFormat(pattern);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String temp = sdf.format(calendar.getTime());
Date tempDate = null;
try {
tempDate = sdf.parse(temp);
} catch (Exception e) {
e.printStackTrace();
}

return dateFormat.format(tempDate);
}

/**
* @Title: getPreviousMonth
* @Description:上个月
* @param
* @return
* @throws
*/
public static Date getPreviousMonth(Date date){
calendar.setTime(date);
calendar.add(Calendar.MONTH, -1);//减一个月
return calendar.getTime();
}


/**
* @Title: getNextMonth
* @Description:下个月
* @param
* @return
* @throws
*/
public static Date getNextMonth(Date date){
calendar.setTime(date);
calendar.add(Calendar.MONTH, 1);//加一个月
return calendar.getTime();
}
}

package com.self;

import java.util.Date;

public class Main_Test {

public static void main (String[] args) {

String date0 = DateUtil.getFirstDayOfMonth(new Date(), "yyyy-MM");

String date1 = DateUtil.getFirstDayOfMonth(new Date(), "yyyy-MM-dd");

String date2 = DateUtil.getFirstDayOfMonth(new Date(), "yyyy-MM-dd HH:mm");

String date3 = DateUtil.getFirstDayOfMonth(new Date(), "yyyy-MM-dd HH:mm:ss");

System.out.println("["+"date0="+date0+",date1="+date1+",date2="+date2+",date3="+date3+"]");


String date4 = DateUtil.getLastDayOfMonth(new Date(), "yyyy-MM");

String date5 = DateUtil.getLastDayOfMonth(new Date(), "yyyy-MM-dd");

String date6 = DateUtil.getLastDayOfMonth(new Date(), "yyyy-MM-dd HH:mm");

String date7 = DateUtil.getLastDayOfMonth(new Date(), "yyyy-MM-dd HH:mm:ss");

System.out.println("["+"date4="+date4+",date5="+date5+",date6="+date6+",date7="+date7+"]");


String date8 = DateUtil.getFirstDayOfYear(new Date(), "yyyy-MM");

String date9 = DateUtil.getFirstDayOfYear(new Date(), "yyyy-MM-dd");

String date10 = DateUtil.getFirstDayOfYear(new Date(), "yyyy-MM-dd HH:mm");

String date11 = DateUtil.getFirstDayOfYear(new Date(), "yyyy-MM-dd HH:mm:ss");

System.out.println("["+"date8="+date8+",date9="+date9+",date10="+date10+",date11="+date11+"]");



String date12 = DateUtil.getLastDayOfMonth(DateUtil.getNextMonth(new Date()), "yyyy-MM");

String date13 = DateUtil.getLastDayOfMonth(DateUtil.getNextMonth(new Date()), "yyyy-MM-dd");

String date14 = DateUtil.getLastDayOfMonth(DateUtil.getNextMonth(new Date()), "yyyy-MM-dd HH:mm");

String date15 = DateUtil.getLastDayOfMonth(DateUtil.getNextMonth(new Date()), "yyyy-MM-dd HH:mm:ss");

System.out.println("["+"date12="+date12+",date13="+date13+",date14="+date14+",date15="+date15+"]");
}
}