Date类
一般用来显示时间
import java.util.Date;
-
String str = new Date().toString();
以字符串形式返回当前时间:Wed Oct 25 09:55:09 CST 2017
Date类提供before();/after();用来比较一个时间点是否早于另一个时间点,其他的方法大多数现已不推荐使用
GregorianCalendar类
public class GregorianCalendar extends Calendar;
import java.util.GregorianCalendar;
- 该类用来进行日期操作
GregorianCalendar(int year, int month, int day)
(0<=month<=11)
GregorianCalendar(int year, int month, int day, int hour, int minutes, int seconds)
(0<=month<=11)
用给定的日期和时间构造一个Gregorian日历对象 -
void set(int year, int month, int day, int hour, int minutes, int seconds)
将日期和时间设置为新的值 -
void add(int field, int value);
将常量(如Calendar.DAY,Calendar.DAY_OF_WEEK,Calendar.DAY_OF_MONTH)加上一个数值,该数值可以为负数 -
void set(int field, int value);
将常量(如Calendar.DAY,Calendar.DAY_OF_WEEK,Calendar.DAY_OF_MONTH)设置为一个新的数值 - 不同地区的日期会不一样,比如一个星期的第一天,可以用函数
Locale.setDefault(Locale.US);
设置为美国日历,或者Locale.CHINA,Locale.ITALY等其他设置。该语句要写在main()方法的第一行。一般虚拟机会根据主机的环境来设置默认地区。
Calendar类
import java.util.Calendar;
-
Calendar calendar = Calendar.getInstance();
Calendar类和单例挺像,但不是单类,因为Calendar可以有多个不同的对象。不能使用new Calendar();
DateFormatSymbols类
-
import java.text.DateFormatSymbols;
String[] getShortWeekdays();
,如MonString[] getWeekdays();
,如MondayString[] getShortMonths();
,如JanString[] getShortMonths();
,如January
分别用来返回Locale指定的地区的星期几简称,星期几,月份名简称,月份名,参数为Calendar类的星期,月份常量。如Calendar.MONDAY,Calendar.JANUARY。
代码示范——本月日历
package javawork;
import java.text.DateFormatSymbols;
import java.util.*;
public class CalendarTest {
public static void main(String[] args){
//set local date
Locale.setDefault(Locale.US);
//construct d as current date
GregorianCalendar d = new GregorianCalendar();
int today = d.get(Calendar.DAY_OF_MONTH);
int month = d.get(Calendar.MONTH);
//set d to start date of the month
d.set(Calendar.DAY_OF_MONTH, 1);
int weekday = d.get(Calendar.DAY_OF_WEEK);
//get first day of week (Sunday in the U.S.)
int firstDayOfWeek = d.getFirstDayOfWeek();
//determine the required indentation for the first line
int indent = 0;
while(weekday != firstDayOfWeek){
indent++;
d.add(Calendar.DAY_OF_MONTH, -1);
weekday = d.get(Calendar.DAY_OF_WEEK);
}
//print weekday names
String[] weekdayNames = new DateFormatSymbols().getShortWeekdays();
do{
System.out.printf("%4s", weekdayNames[weekday]);
d.add(Calendar.DAY_OF_MONTH, 1);
weekday = d.get(Calendar.DAY_OF_WEEK);
}while(weekday != firstDayOfWeek);
System.out.println();
for(int i = 1; i <= indent; i++){
System.out.println(" ");
}
d.set(Calendar.DAY_OF_MONTH, 1);
do{
int day = d.get(Calendar.DAY_OF_MONTH);
System.out.printf("%3d",day);
//mark current day with *
if(day == today) {
System.out.print("*");
}
else{
System.out.printf(" ");
}
d.add(Calendar.DAY_OF_MONTH, 1);
weekday = d.get(Calendar.DAY_OF_WEEK);
//start a new line at the start of the week
if(weekday == firstDayOfWeek){
System.out.println();
}
}while(d.get(Calendar.MONTH ) == month);//the loop exits when d is day 1 of next month
//print final end of line if necessary
if(weekday != firstDayOfWeek){
System.out.println();
}
}
}