review13
Date与Calendar类
Date类和Calendar类属于java.util包。
Date类
1.使用无参数构造方法
使用Date类的无参构造方法创建的对象可以获取本机的当前日期和时间,例如:
Date nowTime = new Date();
2.使用带参数的构造方法
计算机系统将其自身的时间的“公元”设置在1970年1月1日0时(格林威治时间),方法使用情况如下所示:
Date date = new Date(1000);
本地时区是北京时区,“公元”时间是,1970年01月01日08时00分00秒,而上述的date就是1970年01月01日08时00分01秒。
使用System类的静态方法public long currentTimeMillis()获取系统当前时间距离“公元”时间过去的毫秒数。
Calendar类
使用Calendar类的static方法getInstance()可以初始化一个日历对象,如:
Calendar calendar = Calendar.getInstance();
Calendar对象可以调用方法:
public final void set(int year, int month, int date);
public final void set(int year, int month, int date, int hour, int minute);
public final void set(int year, int month, int date, int hour, int minute, int second);
将日历设置在任何一个时间,如:
calendar.set(2014, 5, 25);
就是将日历时间设置在2014年6月25日。
Calendar对象调用方法public int get(int field)可以获取有关年份、月份、小时、星期等信息,参数field的有效值由Calendar的静态常量指定,例如:
calendar.get(Calendar.MONTH);
返回一个整数,如果该整数是0表示当前日历是在1月。
calendar.get(Calendar.DAY_OF_WEEK);
返回一个整数,如果该整数是1表示星期日,7表示星期六。
public long getTimeInMillis()方法获取距离设置的时间的毫秒数。
代码展示如下所示:
public class Test10 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Date nowTime = new Date();
System.out.println(nowTime);
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
System.out.print("现在的时间是: ");
System.out.print(year + "年" + month + "月" + day + "日");
System.out.println(hour + "时" + minute + "分" + second + "秒");
calendar.set(2014, 5, 25);
long time1 = calendar.getTimeInMillis();
calendar.set(2018, 6, 6);
long time2 = calendar.getTimeInMillis();
long subDay = (time2 - time1 ) / (24 * 60 * 60 * 1000);
System.out.println("到现在为止已经经过了" + subDay + "天");
}
}
运行结果如下所示:

review13的更多相关文章
- Ubuntu软件的安装和使用
windows 系统有很多的截图的软件 比如 QQ 浏览器什么的,但是ubuntu 还是很少的截图软件的接下来介绍一下ubuntu 的截图软件 1.键盘上的alt+printscreen 可以全屏幕的 ...
随机推荐
- MVC4学习笔记之--身份认证过滤器
过滤器作为MVC模式中面向切面编程应用很广泛,例如身份验证,日志,异常,行为截取等.博客园里面的大神对应过滤器的介绍以及很多,MVC4中不同的过滤器也介绍得很清楚.FlyDragon 辉太 禁止吸烟 ...
- Martin Fowler’s Active Record design pattern.
P of EAA: Active Record https://www.martinfowler.com/eaaCatalog/activeRecord.html Active Record An o ...
- 自定义log4j日志级别
转载自: http://blog.csdn.net/seven_cm/article/details/26849821 自定义log4j日志级别 参考了网上资料:http://www.360doc. ...
- 随机生成六位验证码函数版(python)
import random def code(n=6,alpha=True): s = '' # 创建字符串变量,存储生成的验证码 for i in range(n): # 通过for循环控制验证码位 ...
- adobe flash player升级coredump分析
flash player版本号:14.0.0.125 产品名称:Adobe® Flash® Player Installer/Uninstaller 系统:windows xp sp3 调试器:win ...
- android密码显示和隐藏
if (showPwd){ //设置EditText文本为可见的 password.setTransformationMethod(HideReturnsTransformationMethod.ge ...
- SpringBoot连接PostgreSQL
这个 org.postgresql.jdbc.PgConnection.createClob() 方法尚未被实作 application.properties spring.datasource.pl ...
- 一种基于自定义代码的asp.net网站访问IP过滤方法!
对于一些企业内部核心系统,特别是外网访问的时候,为了信息安全,可能需要对外部访问的IP地址作限制,虽然IIS中也提供了根据IP地址或IP地址段进行限制或允许,但并没有提供根据IP地址所在的城市进行限制 ...
- Serv-u 外网访问内网的FTP服务器
1. 背景简介 最近研究如何在内网搭架FTP服务器,同时要保证外网(公网)能访问的到.终成正果,但走了一些弯路,在此记下,以飨后人. 2. 基础知识 FTP 使用 2 个端口,一个数据端口和一个命令端 ...
- Linux:文件系统
Linux:文件系统 分区与文件系统 对分区进行格式化是为了在分区上建立文件系统.一个分区通常只能格式化为一个文件系统,但是磁盘阵列等技术可以将一个分区格式化为多个文件系统. 组成 最主要的组成部分如 ...