JAVA基础——时间Date类型转换

在java中有六大时间类,分别是:

1、java.util包下的Date类,

2、java.sql包下的Date类,

3、java.text包下的DateFormat类,(抽象类)

4、java.text包下的SimpleDateFormat类,

5、java.util包下的Calendar类,(抽象类)

6、java.sql包下的Time类,

7、java.sql包下的TimeStamp类。

 

一、java.util包下的Date类:

Date主要用来生成时间,Date有两个构造方法:Date()和Date(long date)

 

 

 

二、java.text包下的DateFormat类,(抽象类):

DateFormat继承自Format。

下面两个是实现的接口,两个接口无任何内容:

DateFormat是时间/日期格式化子类的抽象类,所以不能有构造方法实例化,可以用两个静态函数进行实例化。

getDateInstance()------返回的是日期

getDateTimeInstance()--------------------返回的是时间+日期。

 

 

 

三、java.text包下的SimpleDateFormat类:

SimpleDateFormat继承自DateFormat类,

主要功能是:完成日期之间的格式转换。

yyyy:MM:dd,HH:mm:ss:SSS(SSS是毫秒数)

 

 

 

四、java.util包下的Calendar类,(抽象类):

此类实例化有两种方式:

1、Calendar time=new GregorianCalendar();

2、Calendar time =Calendar.getInstance();

此类和日历相关:“YEAR”年,“MONTH”月,“DAY_OF_MONTH”日,"DAY_OF_WEEK"星期,"HOUR"小时。如:

 

 

 

五、java.sql包下的Date类:

只针对SQL语句使用,Date date=new Date();(也就=2017-01-01,没有时间部分)

 

六、java.sql包下的Time类,

七、java.sql包下的TimeStamp类。

 

 

 

输出结果:

 

 

 

 

输出结果:

 

 

 

 

 

结果:

 

 

 

 

 

结果:

 

 

 

 

 

 

 

 

结果:

 

 1 package test;
 2 
 3 import java.text.DateFormat;
 4 import java.text.ParseException;
 5 import java.text.SimpleDateFormat;
 6 import java.util.Calendar;
 7 import java.util.Date;
 8 import java.util.GregorianCalendar;
 9 
10 public class time {
11     public static void main(String[] args) throws ParseException {
12         // 将当前日期对象转换成毫秒值
13         Date date1 = new Date();
14         Long time1 = date1.getTime();
15         Long time2 = System.currentTimeMillis();// 获取当前时间的毫秒值
16         Calendar cal=Calendar.getInstance();
17         Long caltime=cal.getTimeInMillis();
18         System.out.println("当前日期对象转换成毫秒值:" + time2+"       Calendar类日期转换成毫秒值:"+caltime);
19 
20         // 将毫秒值转换成日期对象
21         Date date2 = new Date();
22         Long time3 = System.currentTimeMillis();
23         date2.setTime(time3);
24 
25         // 将日期字符串转换成日期对象
26         DateFormat df1 = new SimpleDateFormat("yyyy/MM/dd");
27         Date date3 = df1.parse("2017/01/12");
28         System.out.println("日期字符串转换成日期对象:" + date3);
29 
30         SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
31         Date date = df.parse("2009-06-12 02:06:37");
32         System.out.println("日期字符串转换成日期格式:" + df.format(date));
33 
34         // 将日期对象转换成日期字符串
35         Date datetime = new Date();
36         SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
37         String stringtime = sdf.format(datetime);
38         System.out.println("日期对象转换成日期字符串:" + stringtime);
39 
40         Date date4 = new Date();
41         DateFormat df41 = DateFormat.getDateInstance(DateFormat.LONG);
42         DateFormat df42 = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
43         String time4 = df41.format(date4);
44         String time5 = df42.format(date4);
45         System.out.println("getDateInstance时间:" + time4 + "    getDateTimeInstance时间:" + time5);
46 
47         Date d = new Date();
48         DateFormat df43 = DateFormat.getDateInstance(DateFormat.FULL);
49         DateFormat df44 = DateFormat.getDateInstance(DateFormat.SHORT);
50         DateFormat df45 = DateFormat.getDateInstance(DateFormat.MEDIUM);
51         String time43 = df43.format(d);
52         String time44 = df44.format(d);
53         String time45 = df45.format(d);
54         System.out.println("FULL类型时间:" + time43);
55         System.out.println("SHORT类型时间:" + time44);
56         System.out.println("MEDIUM类型时间:" + time45);
57 
58         // 日历
59         Calendar time = Calendar.getInstance();
60         int year = time.get(Calendar.YEAR);
61         int month = time.get(Calendar.MONTH);
62         int day = time.get(Calendar.DAY_OF_MONTH);
63         int week = time.get(Calendar.DAY_OF_WEEK);
64         int hour = time.get(Calendar.HOUR);
65         System.out.println("当前时间是:" + year + "年" + month + "月" + day + "日,星期" + week + "," + hour + "点");
66     }
67 }
1 当前日期对象转换成毫秒值:1504089815431       Calendar类日期转换成毫秒值:1504089815441
2 日期字符串转换成日期对象:Thu Jan 12 00:00:00 CST 2017
3 日期字符串转换成日期格式:2009-06-12 02:06:37
4 日期对象转换成日期字符串:2017/08/30
5 getDateInstance时间:2017年8月30日    getDateTimeInstance时间:2017年8月30日 下午06时43分35秒
6 FULL类型时间:2017年8月30日 星期三
7 SHORT类型时间:17-8-30
8 MEDIUM类型时间:2017-8-30
9 当前时间是:2017年7月30日,星期4,6点

 

转载于:https://www.cnblogs.com/whx20100101/p/7454784.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/391532.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

LeetCode第五天

leetcode 第五天 2018年1月6日 22.(566) Reshape the Matrix JAVA class Solution {public int[][] matrixReshape(int[][] nums, int r, int c) {int[][] newNums new int[r][c];int size nums.length*nums[0].length;if(r*c ! size)return nums;for(int i0;i<size;i){ne…

matplotlib可视化_使用Matplotlib改善可视化设计的5个魔术技巧

matplotlib可视化It is impossible to know everything, no matter how much our experience has increased over the years, there are many things that remain hidden from us. This is normal, and maybe an exciting motivation to search and learn more. And I am sure …

adb 多点触碰_无法触及的神话

adb 多点触碰On Twitter, in Slack, on Discord, in IRC, or wherever you hang out with other developers on the internet, you may have heard some formulation of the following statements:在Twitter上&#xff0c;在Slack中&#xff0c;在Discord中&#xff0c;在IRC…

robot:循环遍历数据库查询结果是否满足要求

使用list类型变量{}接收查询结果&#xff0c;再for循环遍历每行数据&#xff0c;取出需要比较的数值 转载于:https://www.cnblogs.com/gcgc/p/11424114.html

leetcode 74. 搜索二维矩阵(二分)

编写一个高效的算法来判断 m x n 矩阵中&#xff0c;是否存在一个目标值。该矩阵具有如下特性&#xff1a; 每行中的整数从左到右按升序排列。 每行的第一个整数大于前一行的最后一个整数。 示例 1&#xff1a; 输入&#xff1a;matrix [[1,3,5,7],[10,11,16,20],[23,30,34…

rm命令

命令 ‘rm’ &#xff08;remove&#xff09;&#xff1a;删除一个目录中的一个或多个文件或目录&#xff0c;也可以将某个目录及其下属的所有文件及其子目录均删除掉 语法&#xff1a;rm&#xff08;选项&#xff09;&#xff08;参数&#xff09; 默认会提示‘是否’删除&am…

javascript消除字符串两边空格的两种方式,面向对象和函数式编程。python oop在调用时候的优点...

主要是javascript中消除字符串空格&#xff0c;比较两种方式的不同 //面向对象&#xff0c;消除字符串两边空格 String.prototype.trim function() { return this.replace(/(^\s*)|(\s*$)/g, ""); };//去左右空格的函数; function trim(s){return s.replace(/(^\s*)…

如何使用Retrofit,OkHttp,Gson,Glide和Coroutines处理RESTful Web服务

Kriptofolio应用程序系列-第5部分 (Kriptofolio app series — Part 5) These days almost every Android app connects to internet to get/send data. You should definitely learn how to handle RESTful Web Services, as their correct implementation is the core knowle…

leetcode 90. 子集 II(回溯算法)

给你一个整数数组 nums &#xff0c;其中可能包含重复元素&#xff0c;请你返回该数组所有可能的子集&#xff08;幂集&#xff09;。 解集 不能 包含重复的子集。返回的解集中&#xff0c;子集可以按 任意顺序 排列。 示例 1&#xff1a; 输入&#xff1a;nums [1,2,2] 输…

robot:linux下安装robot环境

https://www.cnblogs.com/lgqboke/p/8252488.html 转载于:https://www.cnblogs.com/gcgc/p/11425588.html

感知器 机器学习_机器学习感知器实现

感知器 机器学习In this post, we are going to have a look at a program written in Python3 using numpy. We will discuss the basics of what a perceptron is, what is the delta rule and how to use it to converge the learning of the perceptron.在本文中&#xff0…

JS解析格式化Json插件,Json和XML互相转换插件

Json对象转换为XML字符串插件 http://www.jsons.cn/Down/jquery.json2xml.js var xml_content $.json2xml(json_object);XML字符串转换为Json对象插件 http://www.jsons.cn/Down/jquery.xml2json.js var json_obj $.xml2json(xml_content);json序列化和反序列化方法插件 …

Python之集合、解析式,生成器,函数

一 集合 1 集合定义&#xff1a; 1 如果花括号为空&#xff0c;则是字典类型2 定义一个空集合&#xff0c;使用set 加小括号使用B方式定义集合时&#xff0c;集合内部的数必须是可迭代对象&#xff0c;数值类型的不可以 其中的值必须是可迭代对象&#xff0c;其中的元素必须是可…

深度神经网络课程总结_了解深度神经网络如何工作(完整课程)

深度神经网络课程总结Even if you are completely new to neural networks, this course from Brandon Rohrer will get you comfortable with the concepts and math behind them.即使您是神经网络的新手&#xff0c;Brandon Rohrer的本课程也会使您熟悉其背后的概念和数学。 …

leetcode 1006. 笨阶乘

通常&#xff0c;正整数 n 的阶乘是所有小于或等于 n 的正整数的乘积。例如&#xff0c;factorial(10) 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1。 相反&#xff0c;我们设计了一个笨阶乘 clumsy&#xff1a;在整数的递减序列中&#xff0c;我们以一个固定顺序的操作符序列来…

python:如何传递一个列表参数

转载于:https://www.cnblogs.com/gcgc/p/11426356.html

curl的安装与简单使用

2019独角兽企业重金招聘Python工程师标准>>> windows 篇&#xff1a; 安装篇&#xff1a; 我的电脑版本是windows7,64位&#xff0c;对应的curl下载地址如下&#xff1a; https://curl.haxx.se/download.html 直接找到下面的这个版本&#xff1a; curl-7.57.0.tar.g…

gcc 编译过程

gcc 编译过程从 hello.c 到 hello(或 a.out)文件&#xff0c; 必须历经 hello.i、 hello.s、 hello.o&#xff0c;最后才得到 hello(或a.out)文件&#xff0c;分别对应着预处理、编译、汇编和链接 4 个步骤&#xff0c;整个过程如图 10.5 所示。 这 4 步大致的工作内容如下&am…

虎牙直播电影一天收入_电影收入

虎牙直播电影一天收入“美国电影协会(MPAA)的首席执行官J. Valenti提到&#xff1a;“没有人能告诉您电影在市场上的表现。 直到电影在黑暗的剧院里放映并且银幕和观众之间都散发出火花。 (“The CEO of Motion Picture Association of America (MPAA) J. Valenti mentioned th…

邮箱如何秘密发送多个人邮件_如何发送秘密消息

邮箱如何秘密发送多个人邮件Cryptography is the science of using codes and ciphers to protect messages, at its most basic level. Encryption is encoding messages with the intent of only allowing the intended recipient to understand the meaning of the message.…