java时间设定基础入门

上传人:hao****021 文档编号:166977254 上传时间:2022-11-02 格式:DOCX 页数:23 大小:42.12KB
收藏 版权申诉 举报 下载
java时间设定基础入门_第1页
第1页 / 共23页
java时间设定基础入门_第2页
第2页 / 共23页
java时间设定基础入门_第3页
第3页 / 共23页
资源描述:

《java时间设定基础入门》由会员分享,可在线阅读,更多相关《java时间设定基础入门(23页珍藏版)》请在装配图网上搜索。

1、1 日期为什么是从1970年1月1日I suspect that Java was born and raised on a UNIX system.UNIX considers the epoch (when did time begin) to be midnight, January 1, 1970.是说java起源于UNIX系统,而UNIX认为1970年1月1日0点是时间纪元.但这依然没很好的解释为什么,出于好奇,继续Google,总算找到了答案:这里的解释是:最初计算机操作系统是32位,而时间也是用32位表示。(Integer.MAX_VALUE);2147483647Integer

2、在JAVA内用32位表 示,因此32位能表示的最大值是2147483647。 另外1年365天的总秒数是31536000,2147483647/31536000 = 68.1也就是说32位能表示的最长时间是68年,而实际上到2038年01月19日03时14分07秒,便会到达最大时间,过了这个时间点,所有32位操作系统时间便会变为10000000 00000000 00000000 00000000也就是1901年12月13日20时45分52秒,这样便会出现时间回归的现象,很多软件便会运行异常了。到这里,我想问题的答案已经出来了:因为用32位来表示时间的最大间隔是68年,而最早出现的UNIX操作

3、系统考虑到计算机产生的年代和应用的时限综合取了1970年1月1日作为UNIX TIME的纪元时间(开始时间),而java自然也遵循了这一约束。至于时间回归的现象相信随着64为操作系统的产生逐渐得到解决,因为用64位操作系统可以表示到292,277,026,596年12月4日15时30分08秒,相信我们的N代子孙,哪怕地球毁灭那天都不用愁不够用了,因为这个时间已经是千亿年以后了。最后一个问题:上面(new Date(0),打印出来的时间是8点而非0点,原因是存在系统时间和本地时间的问题,其实系统时间依然是0点,只不过我的电脑时区设置为东8区,故打印的结果是8点。2 BigDecimal类pack

4、age cn.itcast_01;/* * 看程序写结果:结果和我们想的有一点点不一样,这是因为float类型的数据存储和整数不一样导致的。它们大部分的时候,都是带有有效数字位。 * * 由于在运算的时候,float类型和double很容易丢失精度,演示案例。所以,为了能精确的表示、计算浮点数,Java提供了BigDecimal * * BigDecimal类:不可变的、任意精度的有符号十进制数,可以解决数据丢失问题。 */public class BigDecimalDemo public static void main(String args) System.out.println(0.

5、09 + 0.01);System.out.println(1.0 - 0.32);System.out.println(1.015 * 100);System.out.println(1.301 / 100);System.out.println(1.0 - 0.12);构造方法package cn.itcast_02;import ;/* * 构造方法: * public BigDecimal(String val) * * public BigDecimal add(BigDecimal augend) * public BigDecimal subtract(BigDecimal su

6、btrahend) * public BigDecimal multiply(BigDecimal multiplicand) * public BigDecimal divide(BigDecimal divisor) * public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode):商,几位小数,如何舍取 */public class BigDecimalDemo public static void main(String args) / (0.09 + 0.01);/ (1.0 - 0.32);/ (1.

7、015 * 100);/ (1.301 / 100);BigDecimal bd1 = new BigDecimal(0.09);BigDecimal bd2 = new BigDecimal(0.01);System.out.println(add: + bd1.add(bd2);System.out.println(-);BigDecimal bd3 = new BigDecimal(1.0);BigDecimal bd4 = new BigDecimal(0.32);System.out.println(subtract: + bd3.subtract(bd4);System.out.p

8、rintln(-);BigDecimal bd5 = new BigDecimal(1.015);BigDecimal bd6 = new BigDecimal(100);System.out.println(multiply: + bd5.multiply(bd6);System.out.println(-);BigDecimal bd7 = new BigDecimal(1.301);BigDecimal bd8 = new BigDecimal(100);System.out.println(divide: + bd7.divide(bd8);System.out.println(div

9、ide:+ bd7.divide(bd8, 3, BigDecimal.ROUND_HALF_UP);System.out.println(divide:+ bd7.divide(bd8, 8, BigDecimal.ROUND_HALF_UP);3 BigIntegerpackage cn.itcast_01;import ;/* * BigInteger:可以让超过Integer范围内的数据进行运算 * * 构造方法: * BigInteger(String val) */public class BigIntegerDemo public static void main(String

10、args) / 这几个测试,是为了简单超过int范围内,Integer就不能再表示,所以就更谈不上计算了。/ Integer i = new Integer(100);/ (i);/ / (Integer.MAX_VALUE);/ Integer ii = new Integer(2147483647);/ (ii);/ / NumberFormatException/ Integer iii = new Integer(2147483648);/ (iii);/ 通过大整数来创建对象BigInteger bi = new BigInteger(2147483648);System.out.p

11、rintln(bi: + bi);测试package cn.itcast_02;import ;/* * public BigInteger add(BigInteger val):加 * public BigInteger subtract(BigInteger val):减 * public BigInteger multiply(BigInteger val):乘 * public BigInteger divide(BigInteger val):除 * public BigInteger divideAndRemainder(BigInteger val):返回商和余数的数组 */p

12、ublic class BigIntegerDemo public static void main(String args) BigInteger bi1 = new BigInteger(100);BigInteger bi2 = new BigInteger(50);/ public BigInteger add(BigInteger val):加System.out.println(add: + bi1.add(bi2);/ public BigInteger subtract(BigInteger val):加System.out.println(subtract: + bi1.su

13、btract(bi2);/ public BigInteger multiply(BigInteger val):加System.out.println(multiply: + bi1.multiply(bi2);/ public BigInteger divide(BigInteger val):加System.out.println(divide: + bi1.divide(bi2);/ public BigInteger divideAndRemainder(BigInteger val):返回商和余数的数组BigInteger bis = bi1.divideAndRemainder(

14、bi2);System.out.println(商: + bis0);System.out.println(余数: + bis1);3 Calendar 日历Demo1 package cn.itcast_01;import ;/* * Calendar:它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。 * * public int get(int field):返回给定日历字段的值。日历类中的每个日历字段都是静态的成员变量,并且是int类型。 */public cl

15、ass CalendarDemo public static void main(String args) / 其日历字段已由当前日期和时间初始化:Calendar rightNow = Calendar.getInstance(); / 子类对象/ 获取年int year = rightNow.get(Calendar.YEAR);/ 获取月int month = rightNow.get(Calendar.MONTH);/ 获取日int date = rightNow.get(Calendar.DATE);System.out.println(year + 年 + (month + 1)

16、+ 月 + date + 日);/* * abstract class Person public static Person getPerson() return new * Student(); * * class Student extends Person * * */Demo2package cn.itcast_02;import ;/* * public void add(int field,int amount):根据给定的日历字段和对应的时间,来对当前的日历进行操作。 * public final void set(int year,int month,int date):设置

17、当前日历的年月日 */public class CalendarDemo public static void main(String args) / 获取当前的日历时间Calendar c = Calendar.getInstance();/ 获取年int year = c.get(Calendar.YEAR);/ 获取月int month = c.get(Calendar.MONTH);/ 获取日int date = c.get(Calendar.DATE);System.out.println(year + 年 + (month + 1) + 月 + date + 日);/ / 三年前的

18、今天/ c.add(Calendar.YEAR, -3);/ / 获取年/ year = c.get(Calendar.YEAR);/ / 获取月/ month = c.get(Calendar.MONTH);/ / 获取日/ date = c.get(Calendar.DATE);/ (year + 年 + (month + 1) + 月 + date + 日);/ 5年后的10天前c.add(Calendar.YEAR, 5);c.add(Calendar.DATE, -10);/ 获取年year = c.get(Calendar.YEAR);/ 获取月month = c.get(Cale

19、ndar.MONTH);/ 获取日date = c.get(Calendar.DATE);System.out.println(year + 年 + (month + 1) + 月 + date + 日);System.out.println(-);c.set(2011, 11, 11);/ 获取年year = c.get(Calendar.YEAR);/ 获取月month = c.get(Calendar.MONTH);/ 获取日date = c.get(Calendar.DATE);System.out.println(year + 年 + (month + 1) + 月 + date +

20、 日);任意一年的二月有多少天package cn.itcast_03;import ;import ;/* * 获取任意一年的二月有多少天 * * 分析: * A:键盘录入任意的年份 * B:设置日历对象的年月日 * 年就是A输入的数据 * 月是2 * 日是1 * C:把时间往前推一天,就是2月的最后一天 * D:获取这一天输出即可 */public class CalendarTest public static void main(String args) / 键盘录入任意的年份Scanner sc = new Scanner(System.in);(请输入年份:);int year =

21、 sc.nextInt();/ 设置日历对象的年月日Calendar c = Calendar.getInstance();c.set(year, 2, 1); / 其实是这一年的3月1日/ 把时间往前推一天,就是2月的最后一天c.add(Calendar.DATE, -1);/ 获取这一天输出即可(c.get(Calendar.DATE);5 Date 时间Demo1 package cn.itcast_01;import ;/* * Date:表示特定的瞬间,精确到毫秒。 * * 构造方法: * Date():根据当前的默认毫秒值创建日期对象 * Date(long date):根据给定的

22、毫秒值创建日期对象 */public class DateDemo public static void main(String args) / 创建对象Date d = new Date();System.out.println(d: + d);/ 创建对象/ long time = System.currentTimeMillis();long time = 1000 * 60 * 60; / 1小时Date d2 = new Date(time);System.out.println(d2: + d2);Demo2 package cn.itcast_02;import ;/* * pu

23、blic long getTime():获取时间,以毫秒为单位 * public void setTime(long time):设置时间 * * 从Date得到一个毫秒值 * getTime() * 把一个毫秒值转换为Date * 构造方法 * setTime(long time) */public class DateDemo public static void main(String args) / 创建对象Date d = new Date();/ 获取时间long time = d.getTime();System.out.println(time);/ (System.curre

24、ntTimeMillis();System.out.println(d: + d);/ 设置时间d.setTime(1000);System.out.println(d: + d);Demo3.javapackage cn.itcast_03;import ;import ;import ;/* * Date - String(格式化) * public final String format(Date date) * * String - Date(解析) * public Date parse(String source) * * DateForamt:可以进行日期和字符串的格式化和解析,

25、但是由于是抽象类,所以使用具体子类SimpleDateFormat。 * * SimpleDateFormat的构造方法: * SimpleDateFormat():默认模式 * SimpleDateFormat(String pattern):给定的模式 * 这个模式字符串该如何写呢? * 通过查看API,我们就找到了对应的模式 * 年 y * 月 M * 日 d * 时 H * 分 m * 秒 s * * 2014年12月12日 12:12:12 */public class DateFormatDemo public static void main(String args) throw

26、s ParseException / Date - String/ 创建日期对象Date d = new Date();/ 创建格式化对象/ SimpleDateFormat sdf = new SimpleDateFormat();/ 给定模式SimpleDateFormat sdf = new SimpleDateFormat(yyyy年MM月dd日 HH:mm:ss);/ public final String format(Date date)String s = sdf.format(d);(s);/String - DateString str = 2008-08-08 12:12

27、:12;/在把一个字符串解析为日期的时候,请注意格式必须和给定的字符串格式匹配SimpleDateFormat sdf2 = new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);Date dd = sdf2.parse(str);(dd);日期和字符串转换工具package cn.itcast_04;import ;import ;import ;/* * 这是日期和字符串相互转换的工具类 * * author 风清扬 */public class DateUtil private DateUtil() /* * 这个方法的作用就是把日期转成一个字符串 * *

28、param d * 被转换的日期对象 * param format * 传递过来的要被转换的格式 * return 格式化后的字符串 */public static String dateToString(Date d, String format) / SimpleDateFormat sdf = new SimpleDateFormat(format);/ return sdf.format(d);return new SimpleDateFormat(format).format(d);/* * 这个方法的作用就是把一个字符串解析成一个日期对象 * * param s * 被解析的字符串

29、 * param format * 传递过来的要被转换的格式 * return 解析后的日期对象 * throws ParseException */public static Date stringToDate(String s, String format)throws ParseException return new SimpleDateFormat(format).parse(s);工具类测试package cn.itcast_04;import ;import ;/* * 工具类的测试 */public class DateUtilDemo public static void m

30、ain(String args) throws ParseException Date d = new Date();/ yyyy-MM-dd HH:mm:ssString s = DateUtil.dateToString(d, yyyy年MM月dd日 HH:mm:ss);(s);String s2 = DateUtil.dateToString(d, yyyy年MM月dd日);(s2);String s3 = DateUtil.dateToString(d, HH:mm:ss);(s3);String str = 2014-10-14;Date dd = DateUtil.stringTo

31、Date(str, yyyy-MM-dd);(dd);算一下来到这个世界多少天package cn.itcast_05;import ;import ;import ;import ;/* * 算一下你来到这个世界多少天? * * 分析: * A:键盘录入你的出生的年月日 * B:把该字符串转换为一个日期 * C:通过该日期得到一个毫秒值 * D:获取当前时间的毫秒值 * E:用D-C得到一个毫秒值 * F:把E的毫秒值转换为年 * /1000/60/60/24 */public class MyYearOldDemo public static void main(String args)

32、throws ParseException / 键盘录入你的出生的年月日Scanner sc = new Scanner(System.in);(请输入你的出生年月日:);String line = sc.nextLine();/ 把该字符串转换为一个日期SimpleDateFormat sdf = new SimpleDateFormat(yyyy-MM-dd);Date d = sdf.parse(line);/ 通过该日期得到一个毫秒值long myTime = d.getTime();/ 获取当前时间的毫秒值long nowTime = System.currentTimeMillis

33、();/ 用D-C得到一个毫秒值long time = nowTime - myTime;/ 把E的毫秒值转换为年long day = time / 1000 / 60 / 60 / 24;(你来到这个世界: + day + 天);总结1:正则表达式(理解)(1)就是符合一定规则的字符串(2)常见规则A:字符x 字符 x。举例:a表示字符a 反斜线字符。n 新行(换行)符 (u000A) r 回车符 (u000D)B:字符类abc a、b 或 c(简单类) abc 任何字符,除了 a、b 或 c(否定) a-zA-Z a到 z 或 A到 Z,两头的字母包括在内(范围) 0-9 0到9的字符都包

34、括C:预定义字符类. 任何字符。我的就是.字符本身,怎么表示呢? .d 数字:0-9w 单词字符:a-zA-Z_0-9在正则表达式里面组成单词的东西必须有这些东西组成D:边界匹配器 行的开头 $ 行的结尾 b 单词边界就是不是单词字符的地方。举例:hello world?haha;xixiE:Greedy 数量词 X? X,一次或一次也没有X* X,零次或多次X+ X,一次或多次Xn X,恰好 n 次 Xn, X,至少 n 次 Xn,m X,至少 n 次,但是不超过 m 次 (3)常见功能:(分别用的是谁呢?)A:判断功能String类的public boolean matches(Strin

35、g regex)B:分割功能String类的public String split(String regex)C:替换功能String类的public String replaceAll(String regex,String replacement)D:获取功能Pattern和MatcherPattern p = Ppile(a*b);Matcher m = p.matcher(aaaaab);find():查找存不存在group():获取刚才查找过的数据(4)案例A:判断电话号码和邮箱B:按照不同的规则分割数据C:把论坛中的数字替换为*D:获取字符串中由3个字符组成的单词2:Math(掌握

36、)(1)针对数学运算进行操作的类(2)常见方法(自己补齐)A:绝对值B:向上取整C:向下取整D:两个数据中的大值E:a的b次幂F:随机数G:四舍五入H:正平方根(3)案例:A:猜数字小游戏B:获取任意范围的随机数3:Random(理解)(1)用于产生随机数的类(2)构造方法:A:Random() 默认种子,每次产生的随机数不同B:Random(long seed) 指定种子,每次种子相同,随机数就相同(3)成员方法:A:int nextInt() 返回int范围内的随机数B:int nextInt(int n) 返回0,n)范围内的随机数4:System(掌握)(1)系统类,提供了一些有用的字

37、段和方法(2)成员方法(自己补齐)A:运行垃圾回收器B:退出jvmC:获取当前时间的毫秒值D:数组复制5:BigInteger(理解)(1)针对大整数的运算(2)构造方法A:BigInteger(String s)(3)成员方法(自己补齐)A:加B:减C:乘D:除E:商和余数6:BigDecimal(理解)(1)浮点数据做运算,会丢失精度。所以,针对浮点数据的操作建议采用BigDecimal。(金融相关的项目)(2)构造方法A:BigDecimal(String s)(3)成员方法:A:加B:减C:乘D:除E:自己保留小数几位7:Date/DateFormat(掌握)(1)Date是日期类,可

38、以精确到毫秒。A:构造方法Date()Date(long time)B:成员方法getTime()setTime(long time)C:日期和毫秒值的相互转换案例:你来到这个世界多少天了?(2)DateFormat针对日期进行格式化和针对字符串进行解析的类,但是是抽象类,所以使用其子类SimpleDateFormatA:SimpleDateFormat(String pattern) 给定模式yyyy-MM-dd HH:mm:ssB:日期和字符串的转换a:Date - Stringformat()b:String - Dateparse()C:案例:制作了一个针对日期操作的工具类。8:Calendar(掌握)(1)日历类,封装了所有的日历字段值,通过统一的方法根据传入不同的日历字段可以获取值。(2)如何得到一个日历对象呢?Calendar rightNow = Calendar.getInstance();本质返回的是子类对象(3)成员方法A:根据日历字段得到对应的值B:根据日历字段和一个正负数确定是添加还是减去对应日历字段的值C:设置日历对象的年月日(4)案例:计算任意一年的2月份有多少天?

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!