Java日期格式转换.docx
《Java日期格式转换.docx》由会员分享,可在线阅读,更多相关《Java日期格式转换.docx(10页珍藏版)》请在冰豆网上搜索。
Java日期格式转换
时间格式转化与计算代码(java)
2007年04月13日星期五13:
10*
*
Title:
时间格式转化与计算
*/
package;
importjava.util.Calendar;
importjava.util.Date;
importjava.text.*;
publicclassDateTime{
publicDateTime(){
}
/**
*取得系统当前时间
*@returnStringyyyy-mm-dd
*/
publicStringgetCurrentDate(){
CalendarrightNow=Calendar.getInstance();
intyear=rightNow.get(rightNow.YEAR);
intmonth=rightNow.get(rightNow.MONTH)+1;
intday=rightNow.get(rightNow.DATE);
returnyear+"-"+month+"-"+day;
}
/**
*取得系统当前时间
*@returnStringyyyy年mm月dd日
*/
publicStringgetCurrentDate1(){
CalendarrightNow=Calendar.getInstance();
intyear=rightNow.get(rightNow.YEAR);
intmonth=rightNow.get(rightNow.MONTH)+1;
intday=rightNow.get(rightNow.DATE);
returnyear+"年"+month+"月"+day+"日";
}
/**
*取得系统当前时间
*@returnStringyyyymmdd
*/
publicStringgetCurrentDate2(){
CalendarrightNow=Calendar.getInstance();
intyear=rightNow.get(rightNow.YEAR);
intmonth=rightNow.get(rightNow.MONTH)+1;
intday=rightNow.get(rightNow.DATE);
returnyear+""+month+""+day;
}
/**
*取得系统当前时间
*@returnStringyyyy-mm
*/
publicStringgetCurrentDate3(){
CalendarrightNow=Calendar.getInstance();
intyear=rightNow.get(rightNow.YEAR);
intmonth=rightNow.get(rightNow.MONTH)+1;
//intday=rightNow.get(rightNow.DATE);
returnyear+"-"+month;
}
/**
*取得系统当前时间
*@returnStringyyyyMMddhhmmss
*/
publicStringgetCurrentDate4(){
Calendarc=Calendar.getInstance();
c.add(c.SECOND,0);
return""+c.get(c.YEAR)+""+(c.get(c.MONTH)+1)+""+c.get(c.DATE)+""+c.get(c.HOUR_OF_DAY)+""+c.get(c.MINUTE)+""+c.get(c.SECOND);
}
/**
*取得系统当前时间
*@returnStringyyyy-MM-ddhh:
mm:
ss
*/
/*publicStringgetCurrentDate5(){
SimpleDateFormatsimpleDateFormat=newSimpleDateFormat("yyyy-MM-ddhh:
mm:
ss");
java.util.Datedate=newjava.util.Date();
Stringtime=simpleDateFormat.format(date);
returntime;
}
*/
/**
*取得系统当前时间
*@returnStringyyyy-MM-ddhh:
mm:
ss
*/
publicStringgetCurrentDate5(){
Calendarc=Calendar.getInstance();
c.add(c.SECOND,0);
return""+c.get(c.YEAR)+"-"+(c.get(c.MONTH)+1)+"-"+c.get(c.DATE)+""+c.get(c.HOUR_OF_DAY)+":
"+c.get(c.MINUTE)+":
"+c.get(c.SECOND);
}
/**
*取得系统当前时间常用于订单号
*@returnStringyyyyMMddhhmmss
*/
publicStringgetCurrentDate6(){
Calendarc=Calendar.getInstance();
c.add(c.SECOND,0);
Stringyear=c.get(c.YEAR)+"";
return""+year.subSequence(2,year.length())+""+(c.get(c.MONTH)+1)+""+c.get(c.DATE)+"-"+c.get(c.HOUR_OF_DAY)+""+c.get(c.MINUTE)+""+c.get(c.SECOND)+"-"+c.get(c.MILLISECOND);
}
/**
*取得系统当前时间前n个月的相对应的一天
*@paramnint
*@returnStringyyyy-mm-dd
*/
publicStringgetNMonthBeforeCurrentDay(intn){
Calendarc=Calendar.getInstance();
c.add(c.MONTH,-n);
return""+c.get(c.YEAR)+"-"+(c.get(c.MONTH)+1)+"-"+c.get(c.DATE);
}
/**
*取得系统当前时间后n个月的相对应的一天
*@paramnint
*@returnStringyyyy-mm-dd
*/
publicStringgetNMonthAfterCurrentDay(intn){
Calendarc=Calendar.getInstance();
c.add(c.MONTH,n);
return""+c.get(c.YEAR)+"-"+(c.get(c.MONTH)+1)+"-"+c.get(c.DATE);
}
/**
*取得系统当前时间前n天
*@paramnint
*@returnStringyyyy-mm-dd
*/
publicStringgetNDayBeforeCurrentDate(intn){
Calendarc=Calendar.getInstance();
c.add(c.DAY_OF_MONTH,-n);
return""+c.get(c.YEAR)+"-"+(c.get(c.MONTH)+1)+"-"+c.get(c.DATE);
}
/**
*取得系统当前时间后n天
*@paramnint
*@returnStringyyyy-mm-dd
*/
publicStringgetNDayAfterCurrentDate(intn){
Calendarc=Calendar.getInstance();
c.add(c.DAY_OF_MONTH,n);
return""+c.get(c.YEAR)+"-"+(c.get(c.MONTH)+1)+"-"+c.get(c.DATE);
}
//---------------------------------------------------------------------
//取过去一个时间对应的系统当年的一天
publicStringgetCurrentDateAfterPastDate(StringsPastDate){
if(sPastDate!
=null&&!
sPastDate.equals("")){
Datedate=switchStringToDate(sPastDate);
Calendarc=Calendar.getInstance();
c.setTime(date);
intiPastYear=c.get(c.YEAR);
Calendarc1=Calendar.getInstance();
intiCurrentYear=c1.get(c1.YEAR);
c.add(c.YEAR,iCurrentYear-iPastYear);
return""+c.get(c.YEAR)+"-"+(c.get(c.MONTH)+1)+"-"+
c.get(c.DATE);
}
else{
returnnull;
}
}
/**
*将一个日期字符串转化成日期
*@paramsDateString
*@returnDateyyyy-mm-dd
*/
publicDateswitchStringToDate(StringsDate){
Datedate=null;
try{
SimpleDateFormatdf=newSimpleDateFormat("yyyy-MM-dd");
date=df.parse(sDate);
}
catch(Exceptione){
System.out.println("日期转换失败:
"+e.getMessage());
}
returndate;
}
/**
*输入两个字符串型的日期,比较两者的大小
*@paramfromDateString
*@paramtoDateString
*@returnbooleanbefore为true
*/
publicbooleancompareTwoDateBigOrSmall(StringfromDate,StringtoDate){
DatedateFrom=this.switchStringToDate(fromDate);
DatedateTo=this.switchStringToDate(toDate);
if(dateFrom.before(dateTo)){
returntrue;
}
else{
returnfalse;
}
}
/**
*将一个日期字符串转化成Calendar
*@paramsDateString
*@returnCalendar
*/
publicCalendarswitchStringToCalendar(StringsDate){
Datedate=switchStringToDate(sDate);
Calendarc=Calendar.getInstance();
c.setTime(date);
returnc;
}
/**
*将一个日期转化成Calendar
*@paramdateDate
*@returnCalendar
*/
publicCalendarswitchStringToCalendar(Datedate){
Calendarc=Calendar.getInstance();
c.setTime(date);
returnc;
}
publicStringstring2Date(StringdateString)
throwsjava.lang.Exception{
/*DateFormatdateFormat;
dateFormat=newSimpleDateFormat("yyyy-MM-ddkk:
mm:
ss");
dateFormat.setLenient(false);
java.util.DatetimeDate=dateFormat.parse(dateString);//util类型
//java.sql.DatedateTime=newjava.sql.Date(timeDate.getTime());//sql类型
java.sql.TimestampdateTime=newjava.sql.Timestamp(timeDate.getTime());//Timestamp类型
*/
SimpleDateFormatnewk=newSimpleDateFormat("yyyy.MM.ddhh:
mm:
ss");
SimpleDateFormatold=newSimpleDateFormat("MMMddhh:
mm:
ssyyyy");
StringstrDate="Mar2200:
42:
002002";
Dated=old.parse(strDate);
System.out.println(newk.format(d));
returnnewk.format(d);
}
/**
*取得某个时间前n个月的相对应的一天
*@paramsDateString
*@paramnint
*@returnStringyyyy-mm-dd
*/
publicStringgetNMonthBeforeOneDay(StringsDate,intn){
Calendarc=switchStringToCalendar(sDate);
c.add(c.MONTH,-n);
return""+c.get(c.YEAR)+"-"+(c.get(c.MONTH)+1)+"-"+c.get(c.DATE);
}
//取得某个时间后n个月的相对应的一天
publicStringgetNMonthAfterOneDay(StringsDate,intn){
Calendarc=switchStringToCalendar(sDate);
c.add(c.MONTH,n);
return""+c.get(c.YEAR)+"-"+(c.get(c.MONTH)+1)+"-"+c.get(c.DATE);
}
//取得某个时间前n天,格式为yyyy-mm-dd
publicStringgetNDayBeforeOneDate(StringsDate,intn){
Calendarc=switchStringToCalendar(sDate);
c.add(c.DAY_OF_MONTH,-n);
return""+c.get(c.YEAR)+"-"+(c.get(c.MONTH)+1)+"-"+c.get(c.DATE);
}
//取得某个时间后n天,格式为yyyy-mm-dd
publicStringgetNDayAfterOneDate(StringsDate,intn){
Calendarc=switchStringToCalendar(sDate);
c.add(c.DAY_OF_MONTH,n);
return""+c.get(c.YEAR)+"-"+(c.get(c.MONTH)+1)+"-"+c.get(c.DATE);
}
//判断系统当前时间是不是润年
publicbooleanisRunNian(){
java.util.CalendarrightNow=java.util.Calendar.getInstance();
intyear=rightNow.get(rightNow.YEAR);
if(0==year%4&&(year%100!
=0||year%400==0)){
returntrue;
}
else{
returnfalse;
}
}
publicstaticvoidmain(Stringargs[]){
DateTimea=newDateTime();
try{
System.out.println(a.getCurrentDate6());
System.out.println(a.getCurrentDate6());
}catch(Exceptione){System.err.println();}
}