[align=justify;]1.計算某一月份的最大天數
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]time.clear(); [/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]time.set(Calendar.MONTH,i-1);//注意,Calendar對象默認一月為0 [/size][/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]注:在使用set方法之前,必須先clear一下,否則很多信息會繼承自系統當前時間[/size][/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';](1) Calendar轉化為Date[/size][/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]Date date=cal.getTime();[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]Date date=new Date();[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]cal.setTime(date);[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]Date date=new Date();[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]System.out.println(df.format(date));[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';](1)計算某一天是一年中的第幾星期[/size][/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]cal.set(Calendar.YEAR, 2006);[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]cal.set(Calendar.DAY_OF_MONTH, 3);[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';](2)計算一年中的第幾星期是幾號[/size][/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]Calendar cal=Calendar.getInstance();[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]cal.set(Calendar.WEEK_OF_YEAR, 1);[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]System.out.println(df.format(cal.getTime()));[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]2006-01-02[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';](1)add()方法[/size][/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]Calendar cal=Calendar.getInstance();[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]cal.set(Calendar.MONTH,
;[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]cal.add(Calendar.DATE, -4);[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]System.out.println(df.format(date));[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]date=cal.getTime();[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]輸出:[/size][/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]2006-09-03[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]cal.set(Calendar.YEAR, 2006);[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]cal.set(Calendar.DAY_OF_MONTH, 3);[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]date=cal.getTime();[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]cal.roll(Calendar.DATE, 4);[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]System.out.println(df.format(date));[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]2006-09-29[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]可見,roll()方法在本月內循環,一般使用add()方法;[/size][/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';](1)傳進Calendar對象[/size][/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]* @param startday 開始時間[/size][/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]* @return[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]public int getIntervalDays(Calendar startday,Calendar endday)...{[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]if(startday.after(endday))...{[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]startday=endday;[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]}[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]long sl=startday.getTimeInMillis();[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]//根據毫秒數計算間隔天數[/size][/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]}[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]/** *//**計算兩個時間之間相隔天數[/size][/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]* @param endday 結束時間[/size][/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]*/[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]//確保startday在endday之前[/size][/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]Date cal=startday;[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]endday=cal;[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]//分別得到兩個時間的毫秒數[/size][/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]long el=endday.getTime();[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]long ei=el-sl; [/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]return (int)(ei/(1000*60*60*24));[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]注:以上方法是完全按時間計算,有時并不能令人滿意,如:[/size][/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]endday="2006-10-12 8:00:00"[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]在傳參之前,先設定endday的時間,如:[/size][/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]endday.set(Calendar.MINUTE, 59);[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]endday.set(Calendar.MILLISECOND, 59);[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';](3)改進精確計算相隔天數的方法[/size][/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]if (d1.after(d2)) ...{ // swap dates so that d1 is start and d2 is end[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]d1 = d2;[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]}[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]int y2 = d2.get(Calendar.YEAR);[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]d1 = (Calendar) d1.clone();[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);//得到當年的實際天數[/size][/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]} while (d1.get(Calendar.YEAR) != y2);[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]return days;[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]獲取系統當前時間:[/size][/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]Date date=new Date();[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]return df.format(date);[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]String mDateTime1=formatter.format(d);[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]你可以使用dateFormat定義時間日期的格式,轉一個字符串即可[/size][/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]package personal.jessica;[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]import java.util.Calendar;[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]import java.text.DateFormat;[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]import java.util.Locale;[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]/**[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]*@param dateString 需要轉換為timestamp的字符串[/size][/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]*/[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]throws java.text.ParseException {[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss.SSS", Locale.ENGLISH);//設定格式[/size][/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]dateFormat.setLenient(false);[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]java.sql.Timestamp dateTime = new java.sql.Timestamp(timeDate.getTime());//Timestamp類型,timeDate.getTime()返回一個long型[/size][/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]}[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]*method 將字符串類型的日期轉換為一個Date(java.sql.Date)[/size][/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]*@return dataTime Date[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]public final static java.sql.Date string2Date(String dateString)[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]DateFormat dateFormat;[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]dateFormat.setLenient(false);[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]java.sql.Date dateTime = new java.sql.Date(timeDate.getTime());//sql類型[/size][/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]}[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]public static void main(String[] args){[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]//注意:這個地方da.getTime()得到的是一個long型的值[/size][/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]Timestamp t = new Timestamp(new Date().getTime());[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]Timestamp tt = new Timestamp(Calendar.getInstance().get([/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]Calendar.MONTH), Calendar.getInstance().get([/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]Calendar.HOUR), Calendar.getInstance().get([/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]Calendar.SECOND), 0);[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]String sToDate = "2005-8-18";//用于轉換成java.sql.Date的字符串[/size][/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]Date date1 = string2Date(sToDate);[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]System.out.println("Date:"+date1.toString());//結果顯示[/size][/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]}catch(Exception e) {[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]}[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';]}[/size][size=10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes';][/size]
[/size]
[/align]
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
