package demo1;
import java.util.Calendar;
public class TestCalendar {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
//获取时间
int year = calendar.get(Calendar.YEAR);
System.out.println(year);
int month = calendar.get(Calendar.MONTH);
System.out.println(year);
}
}
package demo1;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestDate {
public static void main(String[] args) {
Date date = new Date();
System.out.println(date);
//打印当前是本月第几天
int i = date.getDate();
System.out.println(i);
//打印当前是星期几
int i2= date.getDay();
//月份是从0月开始的
int month = date.getMonth();
System.out.println(month);
//打印当前是今天第几个小时
int hours = date.getHours();
System.out.println(hours);
//获得的是当前年份与1900年份的差值
int year = date.getYear();
System.out.println(year);
//设置月份会对12进行自动取余
//date.setMonth(13);
//得到当前日期 精确到了毫秒
//System.currentTimeMillis();
//new Date
//设置时间格式
SimpleDateFormat format = new SimpleDateFormat("yy-mm-dd");
System.out.println(format.format(new Date()));
System.out.println(format.format(System.currentTimeMillis()));
}
}