packagecom.zjjerp.tool;importjava.text.ParseException;importjava.text.ParsePosition;importjava.text.SimpleDateFormat;importjava.util.Calendar;importjava.util.Date;importjava.util.GregorianCalendar;/*** @Author:Demom
* @Date:2013-8-2,上午9:22:05*/
public classGetDateTimeTools {/***
* 得到yyyy-MM-dd时间格式
*@paramdate
*@return
*/
public staticString getYYYYMMDD(Date date){
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");returnsdf.format(date);
}/***
* 得到yyyy-MM-dd hh:mm:ss时间格式
*@paramdate
*@return
*/
public staticString getYYYYMMDDHHMMSS(Date date){
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");returnsdf.format(date);
}//获取当月的的10号
public staticString getnowMonthtenday(){
Calendar cal=Calendar.getInstance();int month = cal.get(Calendar.MONTH) + 1;int year =cal.get(Calendar.YEAR);
StringBuffer sb=newStringBuffer();
sb.append(year);
sb.append("-");if(month<10){
sb.append("0");
sb.append(month);
}else{
sb.append(month);
}
sb.append("-10");returnsb.toString();
}//获取当前月的下一个月的10号
public staticString getnextMonthtenday(){
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String strDate= sdf.format(newDate());int index = strDate.indexOf(" ");
String str1= strDate.substring(0, index);
String[] str2= str1.split("-");
String nextDay="";if(str2[1]!="12"){int Month = Integer.parseInt(str2[1])+1;
nextDay=str2[0]+"-"+Month+"-"+"10";
}if(str2[1]=="12" || "12".equals(str2[1])){int Month = 1;int Year = Integer.parseInt(str2[0])+1;
nextDay= Year+"-"+Month+"-"+"10";
}returnnextDay;
}//获取当前年
public static intgetCurrentYear(){
Calendar cal=Calendar.getInstance();int month = cal.get(Calendar.MONTH) + 1;int year =cal.get(Calendar.YEAR);returnyear;
}//获取当前yue
public static intgetCurrentMonth(){
Calendar cal=Calendar.getInstance();int month = cal.get(Calendar.MONTH) + 1;returnmonth;
}/*** 以上个月为时间轴,获取上个月的时间
*@paramstatus(0 代表获取月的1号,1代表获取月末)
*@return
*/
public static String getLastMonthDay(intstatus) {
Calendar cal=Calendar.getInstance();
Date date= newDate();
cal.setTime(date);int year = 0;int month = cal.get(Calendar.MONTH); //上个月月份
String newMonth="";//设置年月
if (month == 0) {
year= cal.get(Calendar.YEAR) - 1;
month= 12;
newMonth=""+month;
}else{if(month<10){
newMonth="0"+String.valueOf(month);
}else{
newMonth=String.valueOf(month);
}
year=cal.get(Calendar.YEAR);
}//设置天数
String temp=year + "-" +newMonth.toString();
SimpleDateFormat format= new SimpleDateFormat("yyyy-MM");
Date d= null;try{
d=format.parse(temp);
}catch(ParseException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
cal.setTime(d);int day =cal.getActualMaximum(Calendar.DAY_OF_MONTH);if(status==1){
String endDay= year + "-" + newMonth + "-" +day;returnendDay;
}else{
String endDay= year + "-" + newMonth + "-" + "01";returnendDay;
}
}/*** 以上个月为时间轴,获取下个月的时间
*@paramstatus(0 代表获取月的1号,1代表获取月末)
*@return
*/
public static String getNextMonthDay(intstatus) {
Calendar cal=Calendar.getInstance();int day=cal.get(Calendar.DATE);
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
String timestr= sdf.format(newDate());
timestr=timestr.substring(0,8);if(status==1){
timestr+=String.valueOf(day);
}else{
timestr+="01";
}returntimestr;
}/***
* 以上个月为时间轴,获取当月的时间
*@paramstatus(0 代表获取月的1号,1代表获取月末)
*@return
*/
public static String getCurrMonthDay(intstatus) {
Calendar cal=Calendar.getInstance();
Date date= newDate();
cal.setTime(date);int year = 0;int month = cal.get(Calendar.MONTH); //上个月月份
String newMonth="";//设置年月
if (month == 0) {
year= cal.get(Calendar.YEAR) - 1;
month= 12;
newMonth=""+month;
}else{if(month<10){
newMonth="0"+String.valueOf(month);
}else{
newMonth=String.valueOf(month);
}
year=cal.get(Calendar.YEAR);
}//设置天数
String temp=year + "-" +newMonth.toString();
SimpleDateFormat format= new SimpleDateFormat("yyyy-MM");
Date d= null;try{
d=format.parse(temp);
}catch(ParseException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
cal.setTime(d);int day =cal.getActualMaximum(Calendar.DAY_OF_MONTH);if(status==1){
String endDay= year + "-" + newMonth + "-" +day;returnendDay;
}else{
String endDay= year + "-" + newMonth + "-" + "01";returnendDay;
}
}//获取当月5号日前哦
public staticString getevery5day(){
Calendar cal=Calendar.getInstance();int month = cal.get(Calendar.MONTH) + 1;int year =cal.get(Calendar.YEAR);
StringBuffer sb=newStringBuffer();
sb.append(year);
sb.append("-");if(month<=10){
sb.append("0");
sb.append(month);
}else{
sb.append(month);
}
sb.append("-05");returnsb.toString();
}//合同续约
public static String datexuyue(intyear){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
String now= sdf.format(newDate());
String[] nowStr= now.split("-");int year1 = Integer.parseInt(nowStr[0])+year;
StringBuffer sb= newStringBuffer();
sb.append(year1+"-"+nowStr[1]+"-"+nowStr[2]);returnsb.toString();
}//得到N天后的日期
public static String getBeforeAfterDate(String datestr, intday) {
SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd");
java.sql.Date olddate= null;try{
df.setLenient(false);
olddate= newjava.sql.Date(df.parse(datestr).getTime());
}catch(ParseException e) {throw new RuntimeException("日期转换错误");
}
Calendar cal= newGregorianCalendar();
cal.setTime(olddate);int Year =cal.get(Calendar.YEAR);int Month =cal.get(Calendar.MONTH);int Day =cal.get(Calendar.DAY_OF_MONTH);int NewDay = Day +day;
cal.set(Calendar.YEAR, Year);
cal.set(Calendar.MONTH, Month);
cal.set(Calendar.DAY_OF_MONTH, NewDay);return newjava.sql.Date(cal.getTimeInMillis()).toString();
}//根据两个日期A、B计算AB之间的天数
public static longgetQuot(String time1, String time2){long quot = 0;
SimpleDateFormat ft= new SimpleDateFormat("yyyy-MM-dd");try { Date date1 =ft.parse( time1 );
Date date2=ft.parse( time2 );
quot= date1.getTime() -date2.getTime();
quot= quot / 1000 / 60 / 60 / 24;
}catch(ParseException e) {
e.printStackTrace();
}returnquot;
}//得到N月后的日期
public static String GetSysDate(String format, String StrDate, int year, intmonth,intday) {
Calendar cal=Calendar.getInstance();
SimpleDateFormat sFmt= newSimpleDateFormat(format);
cal.setTime(sFmt.parse( (StrDate),new ParsePosition(0)));if (day != 0) {
cal.add(cal.DATE, day);
}if (month != 0) {
cal.add(cal.MONTH, month);
}if (year != 0) {
cal.add(cal.YEAR, year);
}returnsFmt.format(cal.getTime());
}public static voidmain(String[] args) {
System.out.println(GetSysDate("yyyy-MM-dd", "2004-01-18", 0, 13, 0));
}
}