//Calendar转化为Date
Calendar cal1=Calendar.getInstance();
Date date1=cal1.getTime();
out.print("现在日期是:"+date1+"
");
//Date转化为Calendar
Date date2=new Date();
Calendar cal2=Calendar.getInstance();
cal2.setTime(date2);
out.print("现在日期是:"+date2+"
");
//格式化输出日期时间(常用)
Date date3=new Date();
SimpleDateFormat wsh3=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String time3=wsh3.format(date3);
out.println(time3);
//计算某一天是一年中的第几星期
Calendar cal4=Calendar.getInstance();
cal4.set(Calendar.YEAR, 2009);
cal4.set(Calendar.MONTH,12);
cal4.set(Calendar.DAY_OF_MONTH, 3);
int weekno4=cal4.get(Calendar.WEEK_OF_YEAR);
out.print("这一天是一年中的第几星期:"+weekno4+"
");
//计算一年中的第几星期是几号
SimpleDateFormat wsh5=new SimpleDateFormat("yyyy-MM-dd");
Calendar cal5=Calendar.getInstance();
cal5.set(Calendar.YEAR, 2009);
cal5.set(Calendar.WEEK_OF_YEAR, 1);
cal5.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
out.println("这一天是一年中的第几星期"+wsh5.format(cal5.getTime())+"
");
//String 和 Date ,Long 之间相互转换(最常用)
java.text.SimpleDateFormat wsh6 = new java.text.SimpleDateFormat("M/dd/yyyy hh:mm:ss a",java.util.Locale.US);
java.util.Date d6 = wsh6.parse("12/13/2009 10:31:37 AM");
long dvalue6=d6.getTime();
out.print("d的时间是:"+dvalue6+"
");
SimpleDateFormat formatter6 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String mDateTime6=formatter6.format(d6);
out.print("时间转化为字符串表达为:"+mDateTime6+"
");
//年月周求日期
SimpleDateFormat formatter7 = new SimpleDateFormat("yyyy-MM F E");
java.util.Date date7= formatter7.parse("2009-12 12 星期六");
SimpleDateFormat formatter77 = new SimpleDateFormat("yyyy-MM-dd");
String mydate7=formatter77.format(date7);
out.print("通过年月周求日期为:"+mydate7+"
");
//求是星期几
SimpleDateFormat formatter8 = new SimpleDateFormat("yyyy-MM-dd");
Date mydate8= formatter8.parse("2009-12-13");
SimpleDateFormat formatter88 = new SimpleDateFormat("E");
String mydate9=formatter88.format(mydate8);
out.print("2009-12-13是星期"+mydate9+"
");
//获取服务器端当前日期:
Date myDate = new Date();
out.print("现在服务器上的时间是:"+myDate+"
");
//获取当前年、月、日:
Date myDate9 = new Date();
int thisYear9 = myDate9.getYear() + 1900;
int thisMonth9 = myDate9.getMonth() + 1;
int thisDate9 = myDate9.getDate();
out.print("当前是:"+thisYear9+"年"+thisMonth9+"月"+thisDate9+"日
");
//按本地时区输出当前日期
Date myDate10 = new Date();
out.println(myDate10.toLocaleString());
//获取数据库中字段名为”publish_time“、类型为Datetime的值
//Date sDate = rs.getDate("publish_time");
//按照指定格式打印日期
Date dNow11 = new Date();
SimpleDateFormat formatter11 = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
out.println("It is " + formatter11.format(dNow11)+"
");
//将字符串转换为日期
String input12 = "2009-12-13";
SimpleDateFormat formatter12 = new SimpleDateFormat("yyyy-MM-dd");
Date t12 = null;
t12 = formatter12.parse(input12);
out.println(t12+"
");
//计算日期之间的间隔
String input13 = "2003-05-01";
SimpleDateFormat formatter13 = new SimpleDateFormat("yyyy-MM-dd");
Date d13 = null;
d13 = formatter13.parse(input13);
Date d23 = new Date();
long diff13 = d23.getTime() - d13.getTime();
out.println("Difference is " + (diff13/(1000*60*60*24)) + " days."+"
");
//日期的加减运算
Calendar now14 = Calendar.getInstance();
SimpleDateFormat formatter14 = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
out.println("It is now " + formatter14.format(now14.getTime()));
now14.add(Calendar.DAY_OF_YEAR,-(365*2));
out.println("
");
out.println("Two years ago was " + formatter14.format(now14.getTime())+"
");
//比较日期用equals()、before()、after()方法
DateFormat df15 = new SimpleDateFormat("yyy-MM-dd");
Date d15 = df15.parse("2000-01-01");
Date d25 = df15.parse("1999-12-31");
String relation15 = null;
if(d15.equals(d25))
relation15 = "the same date as";
else if(d15.before(d25))
relation15 = "before";
else
relation15 = "after";
out.println(d15 +" is " + relation15 + ' ' + d25+"
");
//输出结果为:Sat Jan 01 00:00:00 CST 2000 is after Fri Dec 31 00:00:00 CST 1999
//记录一件事所花费的时间 方法:调用两次System.getTimeMillis()方法,求差值
long t16,t17;
t16 = System.currentTimeMillis();
out.println("Cyc starts at " + t16);
int k16 = 0;
for(int i16 =0;i16<100000;i16++){
k16 += i16;
}
t17 = System.currentTimeMillis();
out.println("
");
out.println("Cyc ends at " + t17);
out.println("
");
out.println("This run took " + (t17-t16) + "ms."+"
");
//输出结果为:Cyc starts at 1054275312432 Cyc ends at 1054275312442 This run took 10ms.
//其它:如何格式化小数
DecimalFormat df17 = new DecimalFormat(",###.00");
double aNumber17 = 33665448856.6568975;
String result17 = df17.format(aNumber17);
out.println(result17+"
");
//输出结果为:33,665,448,856.66
//日期比较:
Date date18=new Date();
DateFormat df18=DateFormat.getDateTimeInstance();
String now18=df18.format(date18);
out.println("现在时间:"+now18+"
");
out.println("现在时间是否在16:00之前:"+date18.before(df18.parse("2004-12-24 16:00:00"))+"
");
%>