package packjavacalendardemo;
import javatextSimpleDateFormat;
import javautilCalendar;
import javautilDate;
import javautilGregorianCalendar;
public class CalendarDemo {
private static SimpleDateFormat date_format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
public static void main(String[] args) {
//获取calendar实例;
Calendar calendar = CalendargetInstance();
//判断calendar是不是GregorianCalendar类的实例;
if(calendar instanceof GregorianCalendar){
Systemoutprintln("属于GregorianCalendar类的实例!");
}
//从calendar对象中获得date对象,当前时间;
Date dates = calendargetTime();
//格式化时间;
String date_str= date_formatformat(dates);
Systemoutprintln(date_str);
//设置月份05;代表日历的月份6月,因为月份从0开始。
calendarset(CalendarMONTH, 05);
int months = calendarget(CalendarMONTH);
Systemoutprintln(months); //输出05;
//设置日期为2011-07-24 09:59:50
calendarset(2011, 06, 24, 9, 59, 50);
String getDate = date_formatformat(calendargetTime());
Systemoutprintln(getDate); //输出2011-07-24 09:59:50;
//比较日前大小;
if(new Date()getTime() > calendargetTimeInMillis()){
Systemoutprintln("当前日期在后!");
}else{
Systemoutprintln("当前日期在前!");
}
//设置当前时间为:2011-07-24 11:06:00
calendarsetTime(new Date());
int year = calendarget(CalendarYEAR); //获取年;
int month = calendarget(CalendarMONTH); //获取月;
int date = calendarget(CalendarDATE); //获取天;
int hour = calendarget(CalendarHOUR); //获取小时;
int minute = calendarget(CalendarMINUTE); //获取分钟;
int second = calendarget(CalendarSECOND); //获取秒钟;
int hour_of_day = calendarget(CalendarHOUR_OF_DAY); //第几个小时,
int day_of_month = calendarget(CalendarDAY_OF_MONTH); //这天,在一个月内是第几天
int day_of_week = calendarget(CalendarDAY_OF_WEEK); //这天,在一周内,是第几天
int day_of_year = calendarget(CalendarDAY_OF_YEAR); //这天,在一年内,是第几天。
int week_of_year = calendarget(CalendarWEEK_OF_YEAR); //这周,在一年内是第几周;
int week_of_month = calendarget(CalendarWEEK_OF_MONTH);//这周,在这个月是第几周;以以星为标准;
int zone_offset = calendarget(CalendarZONE_OFFSET); //获取时区;
int day_of_week_in_month = calendarget(CalendarDAY_OF_WEEK_IN_MONTH); //某月中第几周,按这个月1号算,1号起就是第1周,8号起就是第2周。以月份天数为标准
int r = calendarget(CalendarAM_PM);
if(r==calendarAM){
Systemoutprintln("现在是上午");
}
if(r==calendarPM){
Systemoutprintln("现在是下午");
}
Systemoutprintln("==================================================");
Systemoutprintln(year);
Systemoutprintln(month);
Systemoutprintln(date);
Systemoutprintln(hour);
Systemoutprintln(minute);
Systemoutprintln(second);
Systemoutprintln(hour_of_day);
Systemoutprintln(day_of_month);
Systemoutprintln(day_of_week);
Systemoutprintln(day_of_year);
Systemoutprintln(week_of_year);
Systemoutprintln(week_of_month);
Systemoutprintln(zone_offset);
Systemoutprintln(day_of_week_in_month);
}
}
JAVA中 Date 和 Calendar 区别在于:
Date用于记录某一个含日期的、精确到毫秒的时间。重点在代表一刹那的时间本身。
Calendar用于将某一日期放到历法中的互动——时间和年、月、日、星期、上午、下午、夏令时等这些历法规定互相作用关系和互动。Calendar本身代表公历的一个简化缩水版,姑且叫“计算机历”。完整的公历是格里高利历,Java SE中以GregorianCalendar类来提供相关的历法功能。
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//获取前月的第一天
Calendar cal_1=CalendargetInstance();//获取当前日期
cal_1add(CalendarMONTH, -1);
cal_1set(CalendarDAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
firstDay = formatformat(cal_1getTime());
Systemoutprintln("-----1------firstDay:"+firstDay);
//获取前月的最后一天
Calendar cale = CalendargetInstance();
caleset(CalendarDAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天
lastDay = formatformat(calegetTime());
Systemoutprintln("-----2------lastDay:"+lastDay);
//获取当前月第一天:
Calendar c = CalendargetInstance();
cadd(CalendarMONTH, 0);
cset(CalendarDAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
String first = formatformat(cgetTime());
Systemoutprintln("===============first:"+first);
//获取当前月最后一天
Calendar ca = CalendargetInstance();
caset(CalendarDAY_OF_MONTH, cagetActualMaximum(CalendarDAY_OF_MONTH));
String last = formatformat(cagetTime());
Systemoutprintln("===============last:"+last);
以上就是关于java中的Calendar如何使用的全部的内容,包括:java中的Calendar如何使用的、java Date Calendar 区别、怎么利用Calendar获得当月第一天和最后一天的信息等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)