Posted on 2009-07-12 16:59
Gavin.lee 阅读(1416)
评论(0) 编辑 收藏 所属分类:
Date tools
1.date->String
import java.util. * ;
import java.text. * ;
public class DateTest {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
Date time = cal.getTime();
long t = cal.getTimeInMillis();
Date date = new Date();
// time == date
String format1 = new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " ).format(time);
String format2 = new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " ).format(t);
String format3 = new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " ).format(date):
// format1 == format2 == format3
}
}
2.String->date
import java.util. * ;
import java.text. * ;
public class DateTest {
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat ( " yyyy-MM-dd HH:mm:ss " );
Date date = null ;
try {
date = sdf.parse( " 2007-10-03 10:10:10 " ); // date 对象
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(sdf.format(date));
}
}
3.指定格式化日期的语言环境
import java.text.SimpleDateFormat;
import java.util.*;
public class Test {
public static void main(String[] args) {
SimpleDateFormat format = new SimpleDateFormat("EEE", Locale.ENGLISH);
System.out.println(format.format(Calendar.getInstance().getTime())); //Thu
}
}
4.英语下解释时间模式
日期和时间模式
|
结果
|
"yyyy.MM.dd G 'at' HH:mm:ss z"
| 2001.07.04 AD at 12:08:56 PDT
|
"EEE, MMM d, ''yy"
| Wed, Jul 4, '01
|
"h:mm a"
| 12:08 PM
|
"hh 'o''clock' a, zzzz"
| 12 o'clock PM, Pacific Daylight Time
|
"K:mm a, z"
| 0:08 PM, PDT
|
"yyyyy.MMMMM.dd GGG hh:mm aaa"
| 02001.July.04 AD 12:08 PM
|
"EEE, d MMM yyyy HH:mm:ss Z"
| Wed, 4 Jul 2001 12:08:56 -0700
|
"yyMMddHHmmssZ"
| 010704120856-0700
|
"yyyy-MM-dd'T'HH:mm:ss.SSSZ"
| 2001-07-04T12:08:56.235-0700 |