栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java Calendar

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java Calendar

参考:

类的概览 重要解释
	//实例化
		//getInstance()会调用setTimeInMillis(System.currentTimeMillis());
		//最终执行 time = millis; (time是cal的protected long  time 字段)
		Calendar cal = Calendar.getInstance();
		//或者用这一句,效果一样
		cal.setTime(new Date()); // setTimeInMillis(date.getTime()); time =  millis;
	
	//得到当前年份	
		// 下面这句的意思是:得到 实例cal里的time字段的值换算为Calendar.YEAR后的值。注意没有改变cal里面的YEAR字段值
		int year = cal.get(Calendar.YEAR); 
		System.out.println("现在是" + year + "年");//输出: 现在是2021年
		System.out.println("cal是" + cal.YEAR + "年");//输出: cal是1年
		cal.YEAR = 20;//报错	
		//- The final field Calendar.YEAR cannot be assigned
		//- The static field Calendar.YEAR should be accessed in a 
创建流水号

精确到毫秒

// 创建流水号
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH) + 1;// 因为月份从0开始,所以加一
		int day = cal.get(Calendar.DATE);
		int hour = cal.get(Calendar.HOUR_OF_DAY);
		int minute = cal.get(Calendar.MINUTE); // 获取当前分钟
		int second = cal.get(Calendar.SECOND); // 获取当前秒数
		int millisecond = cal.get(Calendar.MILLISECOND); // 获取毫秒数

		String transSerialNo = "xfrs" + String.format("%04d", year)
				+ String.format("%02d", month) + String.format("%02d", day)
				+ String.format("%02d", hour) + String.format("%02d", minute)
				+ String.format("%03d", millisecond);
		System.out.println(transSerialNo);

输出xfrs202110191618204

解释

一些字段如下:

public final static int YEAR = 1;
public final static int MonTH = 2;
public final static int WEEK_OF_YEAR = 3;
public final static int DATE = 5;

protected int           fields[];
protected long          time;

官方说明:
Like other locale-sensitive classes, Calendar provides a

  • class method, getInstance, for getting a generally useful
  • object of this type. Calendar’s getInstance method
  • returns a Calendar object whose
  • calendar fields have been initialized with the current date and time:
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/337588.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号