疯狂java讲义第七章补充习题第12题答案

12、使用Calendar的各种方法

变量
ALL_STYLES
AM
AM_PM
APRIL
areFieldsSet
AUGUST
DECEMBER
DST_OFFSET
ERA
FEBRUARY
FIELD_COUNT
fields
FRIDAY
isSet
isTimeSet
JANUARY
JULY
JUNE
LONG
LONG_FORMAT
LONG_STANDALONE
MARCH
MAY
MONDAY
NARROW_FROMAT
MARROW_STANDALONE
NOVENBER
OCTOBER
PM
SATURDAY
SEPTEMBER
SHORT
SHORT_FORMAT
SHORT_STANDALONE
SUNDAY
THURSDAY
time
TUESDAY
UNDECIMBER
WENDESDAY

以上不知道干嘛用的
DATE
DAY_OF_MONTH
上面这两个效果一样
DAY_OF_WEEK
DAY_OF_WEEK_IN_MONTH//这个不知道和上面有什么区别
DAY_OF_YEAR
HOUR
HOUR_OF_DAY
MILLISECOND
MINUTE
MONTH
SECOND
WEEK_OF_MONTH
WEEK_OF_YEAR
YEAR
ZONE_OFFSET

方法
add()
add()和roll()的区别:add()会修改进位或者后一位,roll()只修改本位。不影响其他位置的时间。
after()
before()
clear() 将时间重置为1971年1月1日
clone()
compareTo()
complete() 不知道怎么用,先放着
computeFields() 不知道怎么用,先放着
computeTime() 不知道怎么用,先放着
equals()
get()
getActualMaximum()
getActualMinimum()
getAvailableCalendarTypes()
getAvailableLocales()
getCalendarType()
getDisplayName()
getDisplayNames()
getFirstDayOfWeek() 不知道怎么用
getGreatestMinimum() 没明白, 和getActualMinimum()还有getMinimum()什么区别?
getInstance() 有一些涉及到TimeZone
getLeastMaximum() 和getMaximum()什么区别??还有getActualMaxinum()
getMaximum()
getMinimalDaysInFirstWeek() 不知道什么意思,?看的不是太明白
getMinimum()
getTime()
getTimeInMillis()
getTimeZone()
getWeeksInWeekYear() 应该是返回一年的总周数,但是WeekYear(周年)是什么意思?
getWeekYear() 和get(YEAR) 啥区别??
hashCode()
internalGet() 不懂怎么用,我也不理解
isLenient()
isSet()方法不理解
isWeekDateSupported() 不理解,怎样算是WeekDateSupport?
roll()
set()
setFirstDayOfWeek() 不理解 不会用
setLenient()
setMinimalDaysInFirstWeek() 不理解,不会用
setTime()
setTimeInMillis()
setTimeZone()
setWeekDate()

import java.util.function.*;
import java.util.stream.*;
import java.util.*;
import java.math.*;
import static  java.util.Calendar.*;
import java.time.*;
public class Test
{
	public static void main(String[] args) throws Exception
	{

		//Calendar
		//构造器
		Calendar calendar = Calendar.getInstance();
		System.out.println(calendar);
		//Calendar和Date相互转换
		//Calendar转Date
		Calendar calendar2 = Calendar.getInstance();
		Date date = calendar2.getTime();
		System.out.println(date);
		//Date转Calendar
		calendar2.setTime(date);	
		System.out.println(calendar2);

		//使用Calendar的变量
		System.out.println(calendar.get(AM));
			
		System.out.println(calendar.get(DATE));
		System.out.println(calendar.get(DAY_OF_MONTH));
		System.out.println(calendar.get(DAY_OF_WEEK));
		System.out.println(calendar.get(DAY_OF_WEEK_IN_MONTH));
		System.out.println(calendar.get(DAY_OF_YEAR));

		System.out.println(calendar.get(ERA));
		System.out.println(calendar.get(HOUR));
		System.out.println(calendar.get(HOUR_OF_DAY));
		System.out.println(calendar.get(MILLISECOND));
		System.out.println(calendar.get(MINUTE));
		System.out.println(calendar.get(MONTH));
		System.out.println(calendar.get(SECOND));
		System.out.println(calendar.get(WEEK_OF_MONTH));
		System.out.println(calendar.get(WEEK_OF_YEAR));
		System.out.println(calendar.get(YEAR));
		System.out.println(calendar.get(ZONE_OFFSET));

		show(calendar);
		calendar.add(YEAR, 1);
		show(calendar);

		//calendar.add(DATE,-10);
		calendar.roll(DATE,-10);
		show(calendar);

		Calendar atime = Calendar.getInstance();
		Calendar btime = Calendar.getInstance();
		btime.add(YEAR,1);
		show(atime);
		show(btime);
		System.out.println(btime.after(atime));	
		System.out.println(btime.before(atime));

		calendar.clear();
		show(calendar);
		
		show(atime);
		atime.clear(MONTH);
		show(atime);

		show(btime);
		btime = (Calendar)atime.clone();
		show(btime);

		atime = Calendar.getInstance();
		btime = Calendar.getInstance();
		//btime.add(YEAR,-1);
		show(atime);
		show(btime);

		System.out.println(atime.compareTo(btime)); 

		show(calendar);

		atime = Calendar.getInstance();
		btime = Calendar.getInstance();
		System.out.println(atime == btime);
		System.out.println(atime.equals(btime));

		System.out.println(atime.get(YEAR)+"-"+atime.get(MONTH));

		System.out.println(atime.getActualMaximum(YEAR));
		System.out.println(atime.getActualMaximum(MONTH));
		System.out.println(atime.getActualMaximum(DATE));

		System.out.println(atime.getActualMaximum(HOUR));
		System.out.println(atime.getActualMaximum(MINUTE));
		System.out.println(atime.getActualMaximum(SECOND));

		System.out.println(atime.getActualMinimum(YEAR));
		System.out.println(atime.getActualMinimum(MONTH));
		System.out.println(atime.getActualMinimum(DATE));
		System.out.println(atime.getActualMinimum(HOUR));
		System.out.println(atime.getActualMinimum(MINUTE));
		System.out.println(atime.getActualMinimum(SECOND));
		/*
		Set<String> set = Calendar.getAvailableCalendarTypes();
		for(String n : set)
		{
			System.out.println(n);
		}
		
		Locale[] locales = Calendar.getAvailableLocales();
		for(Locale l : locales)
		{
			System.out.println(l);
		}
		*/
		System.out.println(atime.getCalendarType());
		System.out.println(atime.getDisplayName(MONTH,LONG,Locale.CHINA));

		Map<String,Integer> map = atime.getDisplayNames(MONTH,LONG,Locale.CHINA);
		for(String n : map.keySet())
		{
			System.out.println(n);
		}

		System.out.println(atime.getFirstDayOfWeek());
		System.out.println(atime.getGreatestMinimum(MONTH));

		Calendar ustime = Calendar.getInstance(Locale.US);
		
		System.out.println(ustime.getFirstDayOfWeek());

		TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
		ustime = Calendar.getInstance(tz , Locale.US);
		System.out.println(ustime.getFirstDayOfWeek());

		System.out.println(ustime.getLeastMaximum(MONTH));
		System.out.println(ustime.getMaximum(MONTH));
	
		System.out.println(ustime.getMinimalDaysInFirstWeek());
		System.out.println(atime.getMinimalDaysInFirstWeek());

		System.out.println(atime.getMinimum(MONTH));

		date = ustime.getTime();
		System.out.println(date);

		System.out.println(atime.getTimeInMillis());
		System.out.println(ustime.getTimeZone());
		System.out.println(atime.getTimeZone());

		System.out.println(atime.getWeeksInWeekYear());
		System.out.println(ustime.getWeeksInWeekYear());

		System.out.println(atime.getWeekYear());

		System.out.println(ustime.hashCode());
		System.out.println(atime.hashCode());

		//System.out.println(atime.internalGet(MONTH));

		System.out.println(atime.isLenient());

		show(atime);
		atime.set(MONTH, 17);
		show(atime);

		System.out.println(atime.isSet(YEAR));
		System.out.println(atime.isWeekDateSupported());

		show(atime);
		atime.roll(MONTH,false);
		show(atime);

		atime.roll(DATE,26);
		show(atime);

		atime.roll(MONTH,-3);
		show(atime);

		atime.set(YEAR,2021);
		atime.set(MONTH,11);
		atime.set(DATE, 5);
		show(atime);

		atime.set(2077,2,18);
		show(atime);

		atime.set(2089,3,5,18,10);
		show(atime);

		atime.set(2099,4, 10 ,8 , 20,15);
		show(atime);

		atime.setFirstDayOfWeek(10);
		System.out.println(atime.getFirstDayOfWeek());

		//atime.setLenient(false);
		//atime.set(MONTH,18); // 这里设置不合理的时间,并不会因为lenient为false而报错
		//show(atime);//只有在这里进行调用的时候才会报错,原因就是set的延迟修改

		atime.setMinimalDaysInFirstWeek(10);
		System.out.println(atime.getMinimalDaysInFirstWeek());

		date = new Date(200);
		atime.setTime(date);
		show(atime);

		atime.setTimeInMillis(1000);
		show(atime);

		atime.setTimeZone(tz);
		atime.setWeekDate(2021,30,5);
		show(atime);

		Instant instant = atime.toInstant();
		System.out.println(instant);
		System.out.println(atime.toString());
		
		
	}
	public static void show(Calendar calendar)
	{
		System.out.println(calendar.get(YEAR)+"-"+(calendar.get(MONTH)+1)+"-"+calendar.get(DATE));
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

细水长流cpu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值