Java学习笔记23(Calendar类)

时间:2022-10-25 15:28:37

Calendar意味日历,对Date类中的很多方法做了改进

Calendar类是一个抽象类,不可以见对象,需要子类完成实现

不过这个类有特殊之处,不需要创建子类对象,而是使用它的静态方法直接获取:

示例:

package demo;

import java.util.Calendar;

public class CalendarDemo {
public static void main(String[] args) { Calendar c1 = Calendar.getInstance();
System.out.println(c1);
}
}

发现输出很多内容,我们仔细看下:

java.util.GregorianCalendar[time=1515655126876,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo
[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null],
firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2018,MONTH=0,WEEK_OF_YEAR=2,
WEEK_OF_MONTH=2,DAY_OF_MONTH=11,DAY_OF_YEAR=11,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=2,AM_PM=1,
HOUR=3,HOUR_OF_DAY=15,MINUTE=18,SECOND=46,MILLISECOND=876,ZONE_OFFSET=28800000,DST_OFFSET=0]

这些和日期有关的内容用一些方法可以得到:

示例:

package demo;

import java.util.Calendar;

public class CalendarDemo {
public static void main(String[] args) {
function();
}
public static void function(){
Calendar c1 = Calendar.getInstance();
int year = c1.get(Calendar.YEAR);
//获取月份时候记得加1,类的内部是0-11月
int month = c1.get(Calendar.MONTH)+1;
int day = c1.get(Calendar.DAY_OF_MONTH);
System.out.println(year+"年"+month+"月"+day+"日");
//输出:2018年1月11日
}
}

其他的一些方法:

package demo;

import java.util.Calendar;
import java.util.Date; public class CalendarDemo {
public static void main(String[] args) {
function1();
function2();
function3();
} public static void function1() {
Calendar c1 = Calendar.getInstance();
c1.set(Calendar.MONTH, 9);
int year = c1.get(Calendar.YEAR);
int month = c1.get(Calendar.MONTH) + 1;
int day = c1.get(Calendar.DAY_OF_MONTH);
System.out.println(year + "年" + month + "月" + day + "日");
// 输出:2018年10月11日
} public static void function2() {
Calendar c1 = Calendar.getInstance();
c1.add(Calendar.DAY_OF_MONTH, 250);
int year = c1.get(Calendar.YEAR);
int month = c1.get(Calendar.MONTH) + 1;
int day = c1.get(Calendar.DAY_OF_MONTH);
System.out.println(year + "年" + month + "月" + day + "日");
// 输出:2018年9月18日
} public static void function3() {
Calendar c1 = Calendar.getInstance();
Date date = c1.getTime();
System.out.println(date);
// 输出:Thu Jan 11 15:36:39 CST 2018
}
}

Calendar类的简单应用:

1.求出自己已经出生多少天:

示例:

package demo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner; //需要知道生日,今日,再做运算即可
public class DateTest {
public static void main(String[] args) throws ParseException {
function();
} public static void function() throws ParseException {
System.out.println("请输入出生日期(格式:YYYY-MM-dd)");
String birthdayString = new Scanner(System.in).next();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
Date birthdayDate = sdf1.parse(birthdayString);
Date todayDate = new Date();
long birthdaySecond = birthdayDate.getTime();
long todaySecond = todayDate.getTime();
long second = todaySecond - birthdaySecond;
if (second < 0) {
System.out.println("还没出生");
} else {
System.out.println(second / 1000 / 60 / 60 / 24);
}
}
}

Java学习笔记23(Calendar类)

2.算闰年(这里不用除4等方法)

示例:

package demo;

import java.util.Calendar;
import java.util.Scanner; public class DateTest {
public static void main(String[] args){
function();
} public static void function(){
Calendar c1 = Calendar.getInstance();
System.out.println("请输入年份");
int year = new Scanner(System.in).nextInt();
c1.set(year, 2, 1);//这里没有错误,2就是3月,类中是0-11月
c1.add(Calendar.DAY_OF_MONTH, -1);
int day = c1.get(Calendar.DAY_OF_MONTH);
if(day == 29){
System.out.println("是闰年");
}else{
System.out.println("不是闰年");
}
}
}

Java学习笔记23(Calendar类)