类 |
描述 |
java.util.Date |
这个类是表示日期和时间。在这个类中的大多数方法已经过时了
|
java.util.concurrent.TimeUnit |
TIMEUNIT是用于说明日期和时间单元的枚举
|
java.sql.Date |
这个类表示日期。切断所有的时间信息。这个日期类在JDBC中使用居多
|
java.sql.Time | Time: 类描述的时间(小时分秒,毫秒),并且不包含日月年的信息。这个类是经常在 JDBC 中使用 |
java.sql.Timestamp |
这个类表示日期和时间。此日期和时间类在JDBC中使用
|
java.util.Calendar |
这是一个Calendar类的基类。有一些方法来做日期和时间算术运算,如添加一天或一个月到另一个日期
|
java.util.GregorianCalendar |
一个java.util.Calendar类的子类,这在大多数西方世界的今天使用的公历表示。拥有所有 java.util.Calendar 类中的方法做,及日期和时间运算
|
java.util.TimeZone |
Java TimeZone类是代表时区的一个类,跨时区做日历算术时是很有帮助的
|
java.text.SimpleDateFormat |
这个类可以帮助您来解析字符串转换日期和时间格式为String
|
package com.yiibai.tutorial.dt; public class JobTimeDemo { // This is the method to sum the numbers from 1 to 100. private static int sum() { int sum = 0; for (int i = 0; i <= 100; i++) { sum += i; } return sum; } private static void doJob(int count) { // Invoke sum method with the number of times // given by the parameter. for (int i = 0; i < count; i++) { sum(); } } public static void main(String[] args) { long millis1 = System.currentTimeMillis(); doJob(10000); long millis2 = System.currentTimeMillis(); long distance = millis2 - millis1; System.out.println("Distance time in milli second: "+ distance); } }
// Minute int minute = 5; // Convert to milliseconds. // This is the traditional way. int millisecond = minute * 60 * 1000; // With TimeUnit: long millisecond = TimeUnit.MINUTES.toMillis(minute);
// Convert to nanoseconds. public long toNanos(long d); // Convert to microseconds public long toMicros(long d); // Convert to miliseconds public long toMillis(long d); // Convert to seconds public long toSeconds(long d); // Convert to minutes public long toMinutes(long d); // Convert to hours public long toHours(long d); // Convert to days public long toDays(long d) ; // Convert to unit specified public long convert(long d, TimeUnit u);
package com.yiibai.tutorial.timeunit; import java.util.concurrent.TimeUnit; public class TimeUnitConvertDemo { public static void main(String[] args) { long second = 125553; // Convert to minute. long minute = TimeUnit.MINUTES.convert(second, TimeUnit.SECONDS); System.out.println("Minute " + minute); // Convert to hours long hour = TimeUnit.HOURS.convert(second, TimeUnit.SECONDS); System.out.println("Hour " + hour); System.out.println("------"); // Convert 3 day to minute minute = TimeUnit.DAYS.toMinutes(3); System.out.println("Minute " + minute); // Convert 3 days to hours hour = TimeUnit.DAYS.toHours(3); System.out.println("Hour " + hour); } }
// Create a Date object describing the current time. Date date1 = new Date(); // Create Date object, millis - the milliseconds since January 1, 1970, 00:00:00 GMT. long millis = .....; Date date2 = new Date(millis);
package com.yiibai.tutorial.date; import java.util.Date; import java.util.concurrent.TimeUnit; public class DateDemo { public static void main(String[] args) throws InterruptedException { // Create a Date object describing the current time. Date date1 = new Date(); // Stop 3 seconds. Thread.sleep(TimeUnit.SECONDS.toMillis(3)); // Returns the current time in milliseconds. // (From 01-01-1970 to now). long millis = System.currentTimeMillis(); Date date2 = new Date(millis); // Compare two objects date1 and date2. // i < 0 means date1 < date2 // i = 0 means date1 = date2 // i > 0 means date1 > date2 int i = date1.compareTo(date2); System.out.println("date1 compareTo date2 = " + i); // Tests if this date is before the specified date. boolean before = date1.before(date2); System.out.println("date1 before date2 ? " + before); // Tests if this date is after the specified date. boolean after = date1.after(date2); System.out.println("date1 after date2 ? " + after); } }
public static Calendar getInstance(); public static Calendar getInstance(TimeZone zone); public static Calendar getInstance(Locale aLocale); public static Calendar getInstance(TimeZone zone,Locale aLocale);
// Get the Calendar object describes the present time. // With Locale default, your TimeZone. Calendar c = Calendar.getInstance();
/** * Gets a calendar using the default time zone and locale. The *Calendar
returned is based on the current time * in the default time zone with the default locale. * * @return a Calendar. */ public static Calendar getInstance() { Calendar cal = createCalendar(TimeZone.getDefaultRef(), Locale.getDefault(Locale.Category.FORMAT)); cal.sharedZone = true; return cal; } /** * Gets a calendar using the specified time zone and default locale. * TheCalendar
returned is based on the current time * in the given time zone with the default locale. * * @param zone the time zone to use * @return a Calendar. */ public static Calendar getInstance(TimeZone zone) { return createCalendar(zone, Locale.getDefault(Locale.Category.FORMAT)); } /** * Gets a calendar using the default time zone and specified locale. * TheCalendar
returned is based on the current time * in the default time zone with the given locale. * * @param aLocale the locale for the week data * @return a Calendar. */ public static Calendar getInstance(Locale aLocale) { Calendar cal = createCalendar(TimeZone.getDefaultRef(), aLocale); cal.sharedZone = true; return cal; } /** * Gets a calendar with the specified time zone and locale. * TheCalendar
returned is based on the current time * in the given time zone with the given locale. * * @param zone the time zone to use * @param aLocale the locale for the week data * @return a Calendar. */ public static Calendar getInstance(TimeZone zone, Locale aLocale) { return createCalendar(zone, aLocale); } private static Calendar createCalendar(TimeZone zone, Locale aLocale) { Calendar cal = null; String caltype = aLocale.getUnicodeLocaleType("ca"); if (caltype == null) { // Calendar type is not specified. // If the specified locale is a Thai locale, // returns a BuddhistCalendar instance. if ("th".equals(aLocale.getLanguage()) && ("TH".equals(aLocale.getCountry()))) { cal = new BuddhistCalendar(zone, aLocale); } else { cal = new GregorianCalendar(zone, aLocale); } } else if (caltype.equals("japanese")) { cal = new JapaneseImperialCalendar(zone, aLocale); } else if (caltype.equals("buddhist")) { cal = new BuddhistCalendar(zone, aLocale); } else { // Unsupported calendar type. // Use Gregorian calendar as a fallback. cal = new GregorianCalendar(zone, aLocale); } return cal; }
get(int)方法 |
返回值 |
get(Calendar.DAY_OF_WEEK) | 1 (Calendar.SUNDAY) to 7 (Calendar.SATURDAY). |
get(Calendar.YEAR) | 年份 |
get(Calendar.MONTH) | 0 (Calendar.JANUARY) to 11 (Calendar.DECEMBER). |
get(Calendar.DAY_OF_MONTH) | 1 - 31 |
get(Calendar.DATE) | 1 - 31 |
get(Calendar.HOUR_OF_DAY) | 0 - 23 |
get(Calendar.MINUTE) | 0 - 59 |
get(Calendar.SECOND) | 0 - 59 |
get(Calendar.MILLISECOND) | 0 - 999 |
get(Calendar.HOUR) | 0 - 11, 能够与 Calendar.AM_PM 一起使用 |
get(Calendar.AM_PM) | 0 (Calendar.AM) 或 1 (Calendar.PM). |
get(Calendar.DAY_OF_WEEK_IN_MONTH) |
1到7总是对应于DAY_OF_MONTH - DAY_OF_WEEK_IN_MONTH1;
8至14对应于DAY_OF_WEEK_IN_MONTH2依此类推。
|
get(Calendar.DAY_OF_YEAR) | 1 - 366 |
get(Calendar.ZONE_OFFSET) |
GMT偏移时区的值
|
get(Calendar.ERA) |
表示AD(阳历Calendar.AD),BC(公历Calendar.BC)
|
package com.yiibai.tutorial.calendar; import java.util.Calendar; public class CalendarFieldsDemo { public static void main(String[] args) { // Create a calendar using the default time zone and locale. Calendar c = Calendar.getInstance(); int year = c.get(Calendar.YEAR); // Returns value from 0 - 11 int month = c.get(Calendar.MONTH); int day = c.get(Calendar.DAY_OF_MONTH); int hour = c.get(Calendar.HOUR_OF_DAY); int minute = c.get(Calendar.MINUTE); int second = c.get(Calendar.SECOND); int millis = c.get(Calendar.MILLISECOND); System.out.println("Year: " + year); System.out.println("Month: " + (month+1)); System.out.println("Day: " + day); System.out.println("Hour: " + hour); System.out.println("Minute: " + minute); System.out.println("Second: " + second); System.out.println("Minute: " + minute); System.out.println("Milli Second: " + millis); } }
void set(int calendarField, int value) void set(int year, int month, int date) void set(int year, int month, int date, int hour, int minute, int second) // Adds or subtracts the specified amount of time to the given calendar field, // based on the calendar's rules. void add(int field, int amount) // Adds or subtracts (up/down) a single unit of time on the // given time field without changing larger fields. void roll(int calendarField, boolean up) // Adds the specified (signed) amount to the specified calendar field // without changing larger fields. void roll(int calendarField, int amount): // return a Date object based on this Calendar's value. Date getTime() void setTime(Date date) // Returns this Calendar's time value in milliseconds. long getTimeInMills(): void setTimeInMillis(long millis) void setTimeZone(TimeZone value)
package com.yiibai.tutorial.calendar; import java.util.Calendar; public class CalendarDemo { public static void showCalendar(Calendar c) { int year = c.get(Calendar.YEAR); // Return value from 0 - 11 int month = c.get(Calendar.MONTH); int day = c.get(Calendar.DAY_OF_MONTH); int hour = c.get(Calendar.HOUR_OF_DAY); int minute = c.get(Calendar.MINUTE); int second = c.get(Calendar.SECOND); int millis = c.get(Calendar.MILLISECOND); System.out.println(" " + year + "-" + (month + 1) + "-" + day + " " + hour + ":" + minute + ":" + second + " " + millis); } public static void main(String[] args) { // Gets a calendar using the default time zone and locale Calendar c = Calendar.getInstance(); System.out.println("First calendar info"); showCalendar(c); // roll(..) does not change other fields. // Roll up one hour (boolean up = true) c.roll(Calendar.HOUR_OF_DAY, true); System.out.println("After roll 1 hour"); showCalendar(c); // roll(..) does not change other fields. // Roll down one hour (boolean up = false) c.roll(Calendar.HOUR_OF_DAY, false); System.out.println("After roll -1 hour"); showCalendar(c); // add(..) can change other fields. // Adding one hour (boolean up = true) c.add(Calendar.HOUR_OF_DAY, 1); System.out.println("After add 1 hour"); showCalendar(c); // roll(..) does not change other fields. // Roll down 30 day (boolean up = false) c.roll(Calendar.DAY_OF_MONTH, -30); System.out.println("After roll -30 day"); showCalendar(c); // add(..) can change other fields. // Adding 30 days (boolean up = true) c.add(Calendar.DAY_OF_MONTH, 30); System.out.println("After add 30 day"); showCalendar(c); } }
Date now = new Date(); Calendar c = Calendar.getInstance(); c.setTime(now);
Calendar c = Calendar.getInstance(); Date date = c.getTime();
package com.yiibai.tutorial.calendar; import java.util.Calendar; import java.util.Date; import java.util.concurrent.TimeUnit; public class CalendarDateConversionDemo { public static void showCalendar(Calendar c) { int year = c.get(Calendar.YEAR); // Returns the value from 0-11 int month = c.get(Calendar.MONTH); int day = c.get(Calendar.DAY_OF_MONTH); int hour = c.get(Calendar.HOUR_OF_DAY); int minute = c.get(Calendar.MINUTE); int second = c.get(Calendar.SECOND); int millis = c.get(Calendar.MILLISECOND); System.out.println(year + "-" + (month + 1) + "-" + day + " " + hour + ":" + minute + ":" + second + " " + millis); } public static void main(String[] args) { Calendar c = Calendar.getInstance(); // year, month, day c.set(2000, 11, 24); Date date = c.getTime(); System.out.println("Date " + date); long timeInMillis = System.currentTimeMillis(); // subtract 24 hours timeInMillis -= TimeUnit.HOURS.toMillis(24); Date date2 = new Date(timeInMillis); Calendar c2 = Calendar.getInstance(); c2.setTime(date2); showCalendar(c2); } }
Date date = new Date(); DateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); String dateString = df.format(date);
String dateString = "23/04/2005 23:11:59"; DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); Date date = df.parse(dateString);
package com.yiibai.tutorial.dateformat; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateFormatDemo { public static void main(String[] args) throws ParseException { final DateFormat df1 = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); String dateString1 = "23/04/2005 23:11:59"; System.out.println("dateString1 = " + dateString1); // String ==> Date Date date1 = df1.parse(dateString1); System.out.println("date1 = " + date1); final DateFormat df2 = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); // Date ==> String. String dateString2 = df2.format(date1); System.out.println("dateString2 = " + dateString2); } }
模式 | 输出 |
---|---|
dd.MM.yy | 30.06.09 |
yyyy.MM.dd G 'at' hh:mm:ss z | 2009.06.30 AD at 08:29:36 PDT |
EEE, MMM d, ''yy | Tue, Jun 30, '09 |
h:mm a | 8:29 PM |
H:mm | 8:29 |
H:mm:ss:SSS | 8:28:36:249 |
K:mm a,z | 8:29 AM,PDT |
yyyy.MMMMM.dd GGG hh:mm aaa | 2009.June.30 AD 08:29 AM |
符号 | 含意 | 呈现 | 示例 |
---|---|---|---|
G | era designator | Text | AD |
y | year | Number | 2009 |
M | month in year | Text & Number | July & 07 |
d | day in month | Number | 10 |
h | hour in am/pm (1-12) | Number | 12 |
H | hour in day (0-23) | Number | 0 |
m | minute in hour | Number | 30 |
s | second in minute | Number | 55 |
S | millisecond | Number | 978 |
E | day in week | Text | Tuesday |
D | day in year | Number | 189 |
F | day of week in month | Number | 2 (2nd Wed in July) |
w | week in year | Number | 27 |
W | week in month | Number | 2 |
a | am/pm marker | Text | PM |
k | hour in day (1-24) | Number | 24 |
K | hour in am/pm (0-11) | Number | 0 |
z | time zone | Text | Pacific Standard Time |
' | escape for text | Delimiter | (none) |
' | single quote | Literal | ' |