Java time and date
Date class
Two constructors:
- Date(): Use the current time to initialize the object
- Date(long millisec): The parameter is the number of milliseconds since January 1, 1970
Date format:
use SimpleDateFormat format
Date date=new Date();
SimpleDateFormat ft=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println(ft.format(date));
SimpleDateFormat format:
G year marker
y year
M month
d day
h hour in the morning or afternoon (1~12)
H hour in the day (0~23)
m minutes
s seconds
S milliseconds
E week
D Day of the year Day
F Day of the week in January
w Day of the year
W Week in January
a Morning/afternoon marker
k hour in the day (1~24)
K hour in the morning or afternoon (0 ~11)
z time zone
Calendar class
Calendar is an abstract classCalendar c=Calendar.getInstance();
Common attributes
- static int YEAR
- static int MONTH:0~11
- static int DATE
- static int HOUR: 12-hour clock
- static int HOUR_OF_DAY: 24-hour clock
- static int MINUTE
- static int SECOND
- static int DAY_OF_WEEK: day of the week
Common method
- set(int field,int value):例:
c.set(Calendar.MONTH,3);
- get (this is the field)
Implementation class GregorianCalendar
Calendar.getInstance()
Is to create and return a GregorianCalendar object.