Java 日期使用

来源:转载

对日期函数进行总结, 主要的API 有

1. java.util.Date

public Date(long date)// 测试此日期是否在指定日期之后。boolean after(Date when)boolean before(Date when)

2. java.text.DateFormat

// 利用特殊的格式构造DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);// 格式化日期String strDate = DateFormat.getDateInstance().format(myDate);// 解析日期字符串,生成Datejava.util.Date = df.parse(StrDate);

3. java.text.SimpleDateFormat

// 用给定的模式和给定语言环境的默认日期格式符号构造 SimpleDateFormatpublic SimpleDateFormat(String pattern,Locale locale)

Letter

Date or Time Component

Presentation

Examples

G

Era designator

Text

AD

y

Year

Year

1996; 96

M

Month in year

Month

July; Jul; 07

w

Week in year

Number

27

W

Week in month

Number

2

D

Day in year

Number

189

d

Day in month

Number

10

F

Day of week in month

Number

2

E

Day in week

Text

Tuesday; Tue

a

Am/pm marker

Text

PM

H

Hour in day (0-23)

Number

0

k

Hour in day (1-24)

Number

24

K

Hour in am/pm (0-11)

Number

0

h

Hour in am/pm (1-12)

Number

12

m

Minute in hour

Number

30

s

Second in minute

Number

55

S

Millisecond

Number

978

z

Time zone

General time zone

Pacific Standard Time; PST; GMT-08:00

Z

Time zone

RFC 822 time zone

-0800

日期和时间模式

结果

"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

4. java.util.Calendar / java.util.GregorianCalendar

//使用给定的 Date 设置此 Calendar 的时间。public final void setTime(Date date);//根据日历的规则,为给定的日历字段添加或减去指定的时间量。//例如,要从当前日历时间减去 5 天,可以通过调用以下方法做到这一点:// add(Calendar.DAY_OF_MONTH, -5)。public abstract void add(int field,int amount)//将指定的字段设置为未定义public final void clear(int field)//返回给定日历字段的值public int get(int field)//返回一个表示此 Calendar 时间值(从历元至现在的毫秒偏移量)的 Date 对象。public final Date getTime()//将给定的日历字段设置为给定值public void set(int field, int value)

5. 常用的函数如下

// java.util.Date -> java.sql.Datepublic static java.sql.Date toSQLDate(java.util.Date date) {if (date == null) {return null;}return new java.sql.Date(date.getTime());}// java.sql.Date -> java.util.Datepublic static java.util.Date toUtilDate(java.sql.Date date) {if (date == null) {return null;}return new java.util.Date(date.getTime());}// 解析dateStr -> java.Util.Datepublic static java.sql.Date toSQLDate(String dateStr) throws ParseException {java.util.Date date = new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);return date;}// 得到指定日期的每个月的开头一天public static java.sql.Date getFirstDayOfMonth(java.sql.Date date) {java.util.Calendar cal = java.util.Calendar.getInstance();cal.setTime(new java.util.Date(date.getTime()));cal.set(Calendar.DAY_OF_MONTH, 1);return new java.sql.Date(cal.getTime().getTime());}// 把指定的日期不常用的字段清零public static Date clearTimeOfDate(Date date) {Calendar cal = Calendar.getInstance();cal.setTimeInMillis(date.getTime());cal.clear(Calendar.AM_PM);cal.clear(Calendar.HOUR);cal.clear(Calendar.HOUR_OF_DAY);cal.clear(Calendar.MINUTE);cal.clear(Calendar.SECOND);cal.clear(Calendar.MILLISECOND);return cal.getTime();}// 判断连个日期是否相等(只比较年月日)public static boolean isSameDay(Date d1, Date d2) {Calendar c1 = Calendar.getInstance();c1.setTime(d1);Calendar c2 = Calendar.getInstance();c2.setTime(d2);if (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR)&& c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH)&& c1.get(Calendar.DAY_OF_MONTH) == c2.get(Calendar.DAY_OF_MONTH)) {return true;}return false;}// 得到当前的日期public static Date getCurrentDate() {return Calendar.getInstance().getTime();}// 比较两个日期之间相差的秒数public static int getSecondDiff(Date date, Date referenceTime) {return Math.round((date.getTime() - referenceTime.getTime()) / (1000));}// 将指定的日期相加 numofMonth月public static Date shiftMonth(Date date, int numOfMonth) {Calendar cl = Calendar.getInstance();cl.setTime(date);cl.add(cl.MONTH, numOfMonth);return cl.getTime();}

分享给朋友:
您可能感兴趣的文章:
随机阅读: