时间日期函数

[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]

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值