Date、Calendar、DateFormat类
Date类与Calendar类之间的转换
package date; import java.util.Calendar;
import java.util.Date; public class DateDemo { public static void main(String[] args) {
/**
* 可以直接输出Date,不可以直接输出Calendar
* 已知当前Date类的日期now,Calendar类的日期calendar,
* 1.将Date--->Calendar,用calendar.setTime(now);
* 2.将Calendar--->Date,那么now=calendar.getTime();
*/
Date now=new Date();
Calendar calendar=Calendar.getInstance();
long now1=System.currentTimeMillis();
long now2=now.getTime();
long now3=calendar.getTimeInMillis();
System.out.println("now :"+now);
System.out.println("calendar.Year :"+calendar.get(Calendar.YEAR));
System.out.println("now1 :"+now1);
System.out.println("now2 :"+now2);
System.out.println("now3 :"+now3); calendar.add(Calendar.YEAR,-19);
System.out.printf("%d-%d-%d\n",calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
//Date-->Calendar
Calendar c1=Calendar.getInstance();//调用Calendar类中的静态方法getInstance()来得到一个Calendar对象
c1.setTime(now); //Calendar-->Date
Date birth=calendar.getTime();
System.out.println("birth :"+birth); System.out.println(birth.toLocaleString()); } }
输出如下内容:
now :Sun Dec 30 11:32:57 CST 2018
calendar.Year :2018
now1 :1546140777528
now2 :1546140777390
now3 :1546140777468
1999-11-30
birth :Thu Dec 30 11:32:57 CST 1999
1999-12-30 11:32:57
DateFormat类:
package date; import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale; public class DateFormatDemo { public static void main(String[] args) {
Date now = new Date(); // Date ==> Calendar
Calendar calendar = Calendar.getInstance();
calendar.setTime(now); // 加8周
calendar.add(Calendar.WEEK_OF_YEAR, 8); // Calendar ==> Date
now = calendar.getTime();
System.out.println(now); // 格式化
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS",Locale.CHINA);
// Date ==> String
String result = format.format(now);//format为DateFormat类中的一个方法,用于Formats a Date into a date/time string.
System.out.println(result); // String ==> Date
String d = "2018-01-12 09:21:21.000";
try {
Date date = format.parse(d);//parse解析,也是DateFormat类中的一个方法,解析给定字符串中的文本内容,以产生一个日期
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
} }
}
Date、Calendar、DateFormat类的更多相关文章
- Java Date Calendar DateFormat Details
From https://www.ntu.edu.sg/home/ehchua/programming/java/DateTimeCalendar.html Date and Time - Creat ...
- 木卯先生的笔记---Date类、DateFormat类和Calendar类
1.Date类 1.1 简介 Date类是 java.util 包下面的类,表示特定的瞬间,精确到毫秒. 1.2 方法 1.2.1 Date() 构造方法 public Date() :分配 Date ...
- Java之DateFormat类
DateFormat类概述 java.text.DateFormat 是日期/时间格式化子类的抽象类,我们通过这个类可以帮我们完成日期和文本之间的转换,也就是可以在Date对象与String对象之间进 ...
- [Day16]常用API(正则表达式、Date类、DateFormat类、Calendar类)
1.正则表达式(Regular Expression,regex)-是一个字符串,使用单个字符串来描述.用来定义匹配规则,匹配一系列符合某个句法规则的字符串 1.1匹配规则: (1)字符:x -代表的 ...
- java时间日期类(Date、DateFormat、Calendar)学习
1.Date类 常用方法:long getTime(),用于返回当前时刻的毫秒值 Date d = new Date(2000); System.out.println(d.getTime());// ...
- 14-03 java BigInteger类,BigDecimal类,Date类,DateFormat类,Calendar类
BigInteger类 发 package cn.itcast_01; import java.math.BigInteger; /* * BigInteger:可以让超过Integer范围内的数据进 ...
- Java 基础 常用API ( 正则表达式,Date类,DateFormat类,Calendar类 )
正则表达式 正则表达式的概念 正则表达式(英语:Regular Expression,在代码中常简写为regex). 正则表达式是一个字符串,使用单个字符串来描述.用来定义匹配规则,匹配一系列符合某个 ...
- Math、Random、System、BigInteger、Date、DateFormat、Calendar类,正则表达式_DAY14
1:Math&大数据类四则运算 X abs(X x) double random() 产生随机数 double ceil(double a) 向上取整 double flo ...
- Java学习(正则表达式、Date类、DateFormat类、Calendar类)
一.正则表达式 1.概念:英语:Regular Expression,在代码中常简写为regex.正则表达式,是一个字符串,使用单个字符串来描述.用来定义匹配规则,匹配一系列符合某个句法规则的字符串. ...
随机推荐
- Confluence 6 为搜索引擎隐藏外部链接
为搜索引擎隐藏外部链接能够避免向你的站点添加垃圾信息.如果你启用了这个选项的话,任何插入到页面中的 URLs 和评论将会赋予 'nofollow' 属性,这个属性将会禁止搜索引擎进行索引. 快捷链接 ...
- 限制 Confluence 6 WebDAV 客户端的写入权限
在早期的 WebDAV 插件中分离了 WebDAV 客户端的写入权限(不能使用,创建/修改,编辑和删除操作)是分开配置的.但是在新版版本的插件中,我们将这些权限合并到了一起. WebDAV 客户端现在 ...
- Confluence 6 SQL Server 数据库驱动修改
从 Confluence 6.4 开始,我们使用官方的 Microsoft SQL Server JDBC 驱动来替换掉开源的 jTDS 驱动.从这个版本开始所有的安装都会默认使用官方的 Micros ...
- JS控制文本框内键盘上下左右键的焦点
avaScript键盘上下左右控制文本框焦点的方法有很多,这里简单说两种方法: 方法一: 创建一个table的dom元素,包含5行4列的文本框 <!DOCTYPE HTML PUBLIC &qu ...
- webpack2配置备份
package.json: { "name": "leyi", "version": "1.0.0", "ma ...
- js中json对象数组按对象属性排序(sort方法)---2(根据拼音排序汉字和排序英文)
本例主要实现 中文汉字按拼音排序的方法和英文按照首字母排序的方法. 要排序的数据: //要排序的数据 let data = [ {chinese: '蔡司', english: 'Chase'}, { ...
- sass方式实现颜色平铺(红色--->紫色)
<!DOCTYPE html><html lang="en"><head> <link rel="stylesheet" ...
- EntityFramework Reverse POCO Code First 反向生成器
https://marketplace.visualstudio.com/items?itemName=SimonHughes.EntityFrameworkReversePOCOGenerator ...
- android app使用微信登录接口回调没有被执行的问题研究
本人开发的一个app使用了sharesdk集成微信登录功能,在测试的过程中微信授权登录界面有调用,但是授权后原应用的回调没有被执行 应用的包名是com.kimi.searcher 首先,确认微信点击授 ...
- Spring data JPA中使用Specifications动态构建查询
有时我们在查询某个实体的时候,给定的条件是不固定的,这是我们就需要动态 构建相应的查询语句,在JPA2.0中我们可以通过Criteria接口查询,JPA criteria查询.相比JPQL,其优势是类 ...