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.正则表达式,是一个字符串,使用单个字符串来描述.用来定义匹配规则,匹配一系列符合某个句法规则的字符串. ...
随机推荐
- 大数据python词频统计之hdfs分发-cacheArchive
-cacheArchive也是从hdfs上进分发,但是分发文件是一个压缩包,压缩包内可能会包含多层目录多个文件 1.The_Man_of_Property.txt文件如下(将其上传至hdfs上) ha ...
- mysql·事务挂起
当开启事务后,程序挂了而事务没有提交,那么会被锁住,报错:连接超时,但不影响查询. 下面操作需要权限 一.查询现在被占用的锁信息 select * from information ...
- WebSocket服务端和客户端使用
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Net;usi ...
- Oracle12c安装和卸载图文教程
注:本文来源于:<Oracle12c安装和卸载图文教程> 一.安装 1.去官网下载相应的版本 2.下载好的两个压缩文件压缩到一个文件夹中 3.打开上个步骤的文件夹,运行stepup,显示如 ...
- 银联支付Java开发
注:原来来源于: < 银联支付Java开发 > 银联的demo写的不错,基本上可以直接使用. 首先是对acp_sdk.properties的内容修改,注意这个文件的文件名不能进行修改. ...
- Confluence 6 配置 Office 转换器
Office 连接器(Office Connector)允许 Confluence 用户能够查看和编辑从 Microsoft Office 和 Open Office 中导入并附加到页面的内容. Of ...
- python修改hosts
#coding=utf-8 host = ['192.168.10.240 store.wondershare.com', '192.168.10.240 store.wondershare.jp', ...
- RabbitMQ中客户端的Channel类里各方法释义
// The contents of this file are subject to the Mozilla Public License // Version 1.1 (the "Lic ...
- js基本类型和字符串的具体应用
变量 JavaScript 是一种弱类型语言,javascript的变量类型由它的值来决定. 定义变量需要用关键字 'var' var a = 123; var b = 'asd'; //同时定义多个 ...
- 史上最简单的SpringCloud教程 | 第四篇:断路器(Hystrix)
在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign来调用.为了保证其高可用,单个服务 ...