java 日期 date_Java Date 日期 时间 相关方法

import java.util.*;import java.text.*;public classDateTime {publicDateTime() {

}public static final long SECOND = 1000;public static final long MINUTE = 60 *SECOND;public static final long HOUR = 60 *MINUTE;public static final long DAY = 24 *HOUR;public static final long WEEK = 7 *DAY;public static final String SHORTFORMAT = "yyyy-MM-dd";public static final String SHORTFORMATNOSPIT = "yyyyMMdd";public static final String YEARMONTHFORMAT = "yyyy-MM";public static final String LONGFORMAT = "yyyy-MM-dd HH:mm:ss";/*** 获得yymmdd类型的日期

*@paramdate

*@return

*/

public staticString getDateString(Date date){

String year=(date.getYear()+1900)+"";

String mm= (date.getMonth()+1)+"";if(Integer.valueOf(mm).intValue()<10){

mm="0"+mm;

}

String day= date.getDate()+"";return year+mm+day;

}/*** 获得yy-mm-dd类型的日期

*@paramdate

*@return

*/

public staticString DateString(Date date){

String year=(date.getYear()+1900)+"";

String mm= (date.getMonth()+1)+"";if(Integer.valueOf(mm).intValue()<10){

mm="0"+mm;

}

String day= date.getDate()+"";if(day.length()==1){

day="0"+day;

}return year+"-"+mm+"-"+day;

}//得到某一天是星期几

public static intgetDateInWeek(String strDate) {

DateFormat df=DateFormat.getDateInstance();try{

df.parse(strDate);

java.util.Calendar c=df.getCalendar();int day = c.get(Calendar.DAY_OF_WEEK) -Calendar.SUNDAY;returnday;

}catch(ParseException e) {return -1;

}

}//得到当前日期

public staticString getCurrentDate() {

java.text.SimpleDateFormat d= newjava.text.SimpleDateFormat();

d.applyPattern("yyyy-MM-dd");

java.util.Date nowdate= newjava.util.Date();

String str_date=d.format(nowdate);returnstr_date;

}//得到当前日期时间

public staticString getCurrentDateTime() {

java.text.SimpleDateFormat d= newjava.text.SimpleDateFormat();

d.applyPattern("yyyy-MM-dd HH:mm:ss");

java.util.Date nowdate= newjava.util.Date();

String str_date=d.format(nowdate);returnstr_date;

}//根据日期获得日期时间

public staticString getDateTime(java.util.Date date) {

java.text.SimpleDateFormat d= newjava.text.SimpleDateFormat();

d.applyPattern("yyyy-MM-dd HH:mm:ss");

String str_date=d.format(date);returnstr_date;

}//获得某月的最后一天

public static int getDayNum(int year, intmonth) {if (month == 2) {return year % 400 != 0 && (year % 4 != 0 || year % 100 == 0) ? 28:29;

}

String SmallMonth= ",4,6,9,11,";returnSmallMonth.indexOf(String.valueOf(String

.valueOf((new StringBuffer(",")).append(String.valueOf(month))

.append(",")))) < 0 ? 31 : 30;

}//返回两个日期之间隔了多少天

public static intDateDiff(Date date1, Date date2) {int i = (int) ( (date1.getTime() - date2.getTime()) / 3600 / 24 / 1000);returni;

}//从放有日期的字符串中得到,相应的年,月,日 style is "Y"or"y" 返回年 style is "M"or"m" 返回月 style//is "D"or"d" 返回日 日期字符串要求 "YYYY-MM-DD"

public static intgetYearMonthDate(String strDate, String style) {intyear;intmonth;intday;intfirstDash;intsecondDash;if (strDate == null) {return 0;

}

firstDash= strDate.indexOf('-');

secondDash= strDate.indexOf('-', firstDash + 1);if ((firstDash > 0) & (secondDash > 0)& (secondDash < strDate.length() - 1)) {

year= Integer.parseInt(strDate.substring(0, firstDash));

month= Integer.parseInt(strDate.substring(firstDash + 1,

secondDash));

day= Integer.parseInt(strDate.substring(secondDash + 1));

}else{return 0;

}if (style.equalsIgnoreCase("Y")) {returnyear;

}else if (style.equalsIgnoreCase("M")) {returnmonth;

}else if (style.equalsIgnoreCase("D")) {returnday;

}else{return 0;

}

}//某一天,过几天后是几号

public static java.sql.Date DateAdd(java.sql.Date date, intaddday) {

java.sql.Date datenew= null;int year = DateTime.getYearMonthDate(date.toString(), "Y");int month = DateTime.getYearMonthDate(date.toString(), "M");int day = DateTime.getYearMonthDate(date.toString(), "D");

day= day +addday;

String dayStr= Integer.toString(year) + "-" + Integer.toString(month) +

"-" +Integer.toString(day);

datenew=datenew.valueOf(dayStr);//datenew.setTime(datenew.getTime()+day*3600*24*1000);//有问题。 改

returndatenew;

}//某一天的前几天是几号

public static java.sql.Date DateBefore(java.sql.Date date, intaddday) {

java.sql.Date datenew= null;int year = DateTime.getYearMonthDate(date.toString(), "Y");int month = DateTime.getYearMonthDate(date.toString(), "M");int day = DateTime.getYearMonthDate(date.toString(), "D");

day= day -addday;

String dayStr= Integer.toString(year) + "-" +Integer.toString(month)+ "-" +Integer.toString(day);

datenew=datenew.valueOf(dayStr);//datenew.setTime(datenew.getTime()+day*3600*24*1000);//有问题。 改

returndatenew;

}//某一天是否是年的头一天

public static booleanisYearFirstDay(java.sql.Date date) {boolean i = false;if ((DateTime.getYearMonthDate(date.toString(), "M") == 1)&& (DateTime.getYearMonthDate(date.toString(), "D") == 1)) {

i= true;

}returni;

}//某一天是否是半年的头一天

public static booleanisHalfYearFirstDay(java.sql.Date date) {boolean i = false;if (((DateTime.getYearMonthDate(date.toString(), "M") == 1) &&(DateTime

.getYearMonthDate(date.toString(),"D") == 1))|| ((DateTime.getYearMonthDate(date.toString(), "M") == 7) &&(DateTime

.getYearMonthDate(date.toString(),"D") == 1))) {

i= true;

}returni;

}public staticString getHalfYearFirstDay(java.sql.Date date) {

String month= "01";if (DateTime.getYearMonthDate(date.toString(), "M") >= 7) {

month= "07";

}

String day=Integer.toString(DateTime.getYearMonthDate(date

.toString(),"Y"))+ "-" + month + "-01";returnday;

}//某一天是否是半年的最后一天

public static booleanisHalfYearLastDay(java.sql.Date date) {boolean i = false;if (((DateTime.getYearMonthDate(date.toString(), "M") == 12) &&(DateTime

.getYearMonthDate(date.toString(),"D") == 31))|| ((DateTime.getYearMonthDate(date.toString(), "M") == 6) &&(DateTime

.getYearMonthDate(date.toString(),"D") == 30))) {

i= true;

}returni;

}public staticString getHalfYearLastDay(java.sql.Date date) {

String month= "-06-30";if (DateTime.getYearMonthDate(date.toString(), "M") >= 7) {

month= "-12-31";

}

String day= Integer.toString(getYearMonthDate(date.toString(), "Y"))+ "-" +month;returnday;

}//某一天是否是年的最后一天

public static booleanisYearLastDay(java.sql.Date date) {boolean i = false;if ((DateTime.getYearMonthDate(date.toString(), "M") == 12)&& (DateTime.getYearMonthDate(date.toString(), "D") == 31)) {

i= true;

}returni;

}//某一天是否是季的头一天

public static booleanisQuarterFirstDay(java.sql.Date date) {boolean i = false;if (((DateTime.getYearMonthDate(date.toString(), "M") == 1)|| (DateTime.getYearMonthDate(date.toString(), "M") == 4)|| (DateTime.getYearMonthDate(date.toString(), "M") == 7) ||(DateTime

.getYearMonthDate(date.toString(),"M") == 10))&& (DateTime.getYearMonthDate(date.toString(), "D") == 1)) {

i= true;

}returni;

}public staticString getQuarterFirstDay(java.sql.Date date) {

String month= "01";if (DateTime.getYearMonthDate(date.toString(), "M") >= 10) {

month= "10";

}else if (DateTime.getYearMonthDate(date.toString(), "M") >= 7) {

month= "07";

}else if (DateTime.getYearMonthDate(date.toString(), "M") >= 4) {

month= "04";

}else if (DateTime.getYearMonthDate(date.toString(), "M") >= 1) {

month= "01";

}

String day=Integer.toString(DateTime.getYearMonthDate(date

.toString(),"Y"))+ "-" + month + "-01";returnday;

}//某一天是否是季的最后一天

public static booleanisQuarterLastDay(java.sql.Date date) {boolean i = false;if ((DateTime.getYearMonthDate(date.toString(), "M") == 3)&& (DateTime.getYearMonthDate(date.toString(), "D") == 31)) {

i= true;

}if ((DateTime.getYearMonthDate(date.toString(), "M") == 6)&& (DateTime.getYearMonthDate(date.toString(), "D") == 30)) {

i= true;

}if ((DateTime.getYearMonthDate(date.toString(), "M") == 9)&& (DateTime.getYearMonthDate(date.toString(), "D") == 30)) {

i= true;

}if ((DateTime.getYearMonthDate(date.toString(), "M") == 12)&& (DateTime.getYearMonthDate(date.toString(), "D") == 31)) {

i= true;

}returni;

}public staticString getQuarterLastDay(java.sql.Date date) {

String month= "-01-31";if (DateTime.getYearMonthDate(date.toString(), "M") >= 10) {

month= "-12-31";

}else if (DateTime.getYearMonthDate(date.toString(), "M") >= 7) {

month= "-09-30";

}else if (DateTime.getYearMonthDate(date.toString(), "M") >= 4) {

month= "-06-30";

}

String day=Integer.toString(DateTime.getYearMonthDate(date

.toString(),"Y"))+ "-" +month;returnday;

}//某一天是否是月的最后一天

public static booleanisMonthLastDay(java.sql.Date date) {boolean i = false;

java.sql.Date des_date= null;

String month;

String str_date=date.toString();

String year= str_date.substring(0, str_date.indexOf("-"));int m = new Integer(str_date.substring(str_date.indexOf("-") + 1,

str_date.lastIndexOf("-"))).intValue() + 1;

month= newInteger(m).toString();if (m < 10) {

month= "0" +month;

}

java.sql.Date mid_date= null;

mid_date= mid_date.valueOf(year + "-" + month + "-01");

des_date= DateTime.DateAdd(mid_date, -1);if (DateTime.DateDiff(des_date, date) == 0) {

i= true;

}returni;

}//某一天是否是月的第一天

public static booleanisMonthFisrtDay(java.sql.Date date) {boolean i = false;if ((DateTime.getYearMonthDate(date.toString(), "D") == 1)) {

i= true;

}returni;

}//获得月的第一天

public staticString getMonthFisrtDay(java.sql.Date date)

{

String month;if (DateTime.getYearMonthDate(date.toString(), "M") > 9) {

month=Integer.toString(DateTime.getYearMonthDate(date

.toString(),"M"));

}else{

month= "0"

+Integer.toString(DateTime.getYearMonthDate(date

.toString(),"M"));

}

String day=Integer.toString(DateTime.getYearMonthDate(date

.toString(),"Y"))+ "-" + month + "-01";returnday;

}public staticjava.sql.Timestamp getTimestamp() {try{

java.text.SimpleDateFormat myFormat= newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");

java.util.Calendar calendar=java.util.Calendar.getInstance();

String mystrdate=myFormat.format(calendar.getTime());returnjava.sql.Timestamp.valueOf(mystrdate);

}catch(Exception e) {return null;

}

}public staticjava.sql.Timestamp getTimestamp(String datestr) {try{

java.text.SimpleDateFormat myFormat= newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String mystrdate=myFormat.format(myFormat.parse(datestr));returnjava.sql.Timestamp.valueOf(mystrdate);

}catch(Exception e) {return null;

}

}//格式化日期(Y-年,M-月,D-日 HH:mm:ss 小时:分钟:秒)

public staticString getDate(java.util.Date date, String format) {

String result= null;try{

java.text.SimpleDateFormat myFormat= newSimpleDateFormat(format);

result=myFormat.format(date);

}catch(Exception e) {return null;

}returnresult;

}public staticString getDate(java.util.Date date) {returngetDate(date, LONGFORMAT);

}//转换成时间戳

public staticjava.sql.Timestamp getDate(String datestr) {try{

java.text.SimpleDateFormat myFormat= newSimpleDateFormat("yyyy-MM-dd");

Date date=myFormat.parse(datestr);

myFormat.applyLocalizedPattern("yyyy-MM-dd HH:mm:ss");

String mystrdate=myFormat.format(date);returnjava.sql.Timestamp.valueOf(mystrdate);

}catch(Exception e) {return null;

}

}//将日期格式化成yyyy-MM-dd形式

public staticjava.util.Date format(String datestr) {try{

java.text.SimpleDateFormat myFormat= newSimpleDateFormat("yyyy-MM-dd");

Date date=myFormat.parse(datestr);returndate;

}catch(Exception e) {return newDate(datestr);

}

}//格式化日期(Y-年,M-月,D-日 HH:mm:ss 小时:分钟:秒)

public staticjava.util.Date format(String datestr, String format) {try{

java.text.SimpleDateFormat myFormat= newSimpleDateFormat(format);

Date date=myFormat.parse(datestr);returndate;

}catch(Exception e) {return null;

}

}//获得数据库格式的日期

public staticjava.sql.Date getSqlDate(java.util.Date date) {

java.sql.Date result= null;try{

java.text.SimpleDateFormat myFormat= newSimpleDateFormat("yyyy-MM-dd");

String mystrdate=myFormat.format(date);

result=java.sql.Date.valueOf(mystrdate);

}catch(Exception e) {return null;

}returnresult;

}public static java.util.Date addMonth(java.util.Date date, intmonth) {

String strdate=getDate(date, SHORTFORMAT);int curmonth = Integer.parseInt(strdate.substring(5, 7));int year = Integer.parseInt(strdate.substring(0, 4));int addyear = month / 12;

year= year +addyear;

curmonth= curmonth + (month % 12);if (curmonth > 12) {

curmonth= 1;

year= year + 1;

}

String strmonth=String.valueOf(curmonth);if (strmonth.length() == 1) {

strmonth= "0" +strmonth;

}

strdate= String.valueOf(year) + "-" + strmonth + "-"

+ strdate.substring(8, 10);returnformat(strdate, SHORTFORMAT);

}/*** 传递日期, 获得上个月的最后1天

*@paramdt

*@return

*/

public staticString getUpMDate(Date dt){

dt.setDate(1);

dt.setDate(dt.getDate()-1);returndt.toLocaleString();

}/***得到当前是那一天。

**/

public staticString getDate() {

java.text.SimpleDateFormat d= newjava.text.SimpleDateFormat();

d.applyPattern("yyyy-MM-dd");

java.util.Date nowdate= newjava.util.Date();

String str_date=d.format(nowdate);returnstr_date;

}/*** 将10位日期格式化为8位

*@paramdt

*@return

*/

public staticString getShortDate(String dt){if(dt != null){

java.text.SimpleDateFormat myFormat= new SimpleDateFormat("yyyy-MM-dd");try{

Date date=myFormat.parse(dt);returngetDate(date,SHORTFORMATNOSPIT);

}catch(ParseException e) {returndt;

}

}else

returndt;

}/*** 将8位日期格式化为10位

*@paramdt

*@return

*/

public staticString getLongDate(String dt){if(dt != null){

java.text.SimpleDateFormat myFormat= new SimpleDateFormat("yyyyMMdd");try{

Date date=myFormat.parse(dt);returngetDate(date,SHORTFORMAT);

}catch(ParseException e) {returndt;

}

}else

returndt;

}/*** 判断是否是当月

*@parammonth

*@return

*/

public static booleanisSameYearMonth(String date){try{

String currdate=getCurrentDate();

currdate= getShortDate(currdate).substring(0,6);

String lastdate= getShortDate(date).substring(0,6);if(lastdate.equals(currdate)){return true;

}else{return false;

}

}catch(NumberFormatException e){return false;

}

}public static voidmain(String[] args){

Date d= new Date("20070829");

Date f= new Date("20070912");

System.out.println(d.getDate()-f.getDate());

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值