java 中Date类型的时间显示格式
来源:互联网 发布:万网域名证书下载 编辑:程序博客网 时间:2024/06/05 17:46
本文转至http://www.blogjava.net/sharky/articles/13057.html
创建一个日期对象
使用系统的当前日期和时间创建一个日期对象并返回一个长整数的简单例子。 这个时间通常被称为Java 虚拟机(JVM)主机环境的系统时间。
import java.util.Date;
public class DateExample1 { public static void main(String[] args) { // Get the system date/time Date date = new Date(); System.out.println(date.getTime()); } }
今天是星期一,2005年8月8日,上午8:43,上面的例子在系统输出设备上显示的结果是1123461832312。
日期数据的定制格式
使用类java.text.SimpleDateFormat和它的抽象基类 java.text.DateFormat 完成日期数据的格式定制,比方今天星期一-八月-08-2005。下面的例子展示了如何完成这个工作:
import java.text.SimpleDateFormat; import java.util.Date;
public class DateExample2 {
public static void main(String[] args) { SimpleDateFormat bartDateFormat = new SimpleDateFormat ("EEEE-MMMM-dd-yyyy"); Date date = new Date(); System.out.println(bartDateFormat.format(date)); } }
只要通过向SimpleDateFormat 的构造函数传递格式字符串"EEE-MMMM-dd-yyyy",就能够指明自己想要的格式。运行结果就是:星期一-八月-08-2005 了。传递"EE-MM-dd-yy"会显示 星期一-08-08-05 。
将文本数据解析成日期对象
假设一个文本字符串包含了一个格式化了的日期对象,而需要解析这个字符串并从文本日期数据创建一个日期对象。下面的例子,将解析文本字符串"8-8-2005"并创建一个值为1123430400000 的日期对象。
例子程序:
import java.text.SimpleDateFormat; import java.util.Date;
public class DateExample3{ public static void main(String[] args) { // Create a date formatter that can parse dates of the form MM-dd-yyyy. SimpleDateFormat bartDateFormat = new SimpleDateFormat("MM-dd-yyyy"); // Create a string containing a text date to be parsed. String dateStringToParse = "8-8-2005"; try { // Parse the text version of the date. //We have to perform the parse method in a //try-catch construct in case dateStringToParse //does not contain a date in the format we are expecting. Date date = bartDateFormat.parse(dateStringToParse); // Now send the parsed date as a long value // to the system output. System.out.println(date.getTime()); } catch (Exception ex){ System.out.println(ex.getMessage()); } } }
使用标准的日期格式化过程
可以生成和解析定制的日期格式后,现在来看一看如何使用内建的格式化过程。使用方法DateFormat.getDateTimeInstance()可以得到用几种不同的方法获得标准的日期格式化过程。在下面的例子中,我们获取了四个内建的日期格式化过程。它们包括一个短的,中等的,长的,和完整的日期格式。
import java.text.DateFormat; import java.util.Date;
public class DateExample4{ public static void main(String[] args) { Date date = new Date(); DateFormat shortDateFormat = DateFormat.getDateTimeInstance (DateFormat.SHORT,DateFormat.SHORT); DateFormat mediumDateFormat = DateFormat.getDateTimeInstance (DateFormat.MEDIUM,DateFormat.MEDIUM); DateFormat longDateFormat = DateFormat.getDateTimeInstance (DateFormat.LONG,DateFormat.LONG); DateFormat fullDateFormat = DateFormat.getDateTimeInstance DateFormat.FULL,DateFormat.FULL);
System.out.println(shortDateFormat.format(date)); System.out.println(mediumDateFormat.format(date)); System.out.println(longDateFormat.format(date)); System.out.println(fullDateFormat.format(date)); } }
注意我们在对 getDateTimeInstance的每次调用中都传递了两个值。 第一个参数是日期风格, 而第二个参数是时间风格。 它们都是基本数据类型int(整型)。考虑到可读性,这里使用了DateFormat 类提供的常量: SHORT, MEDIUM, LONG, 和 FULL。
运行例子程序, 它将向标准输出设备输出下面的内容:
05-8-8 上午9:17 2005-8-8 9:17:42 2005年8月8日 上午09时17分42秒2005年8月8日 09时17分42秒 GMT+08:00
补充:如果你想要显示“2010年08月22日 星期日 23:55:26” 可以使用
Date dd = new Date();SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 EEE HH:mm:ss");dt = sdf.format(dd);
还有一点hh:mm:ss显示的是12制的时间,HH:mm:ss 显示的是24制的时间
- java 中Date类型的时间显示格式
- java 中Date类型的时间显示格式
- Date类型的时间显示格式
- Java中带有T Z格式(UTC是世界标准时间)的时间转换为date,string,long类型
- java Date类型时间转字符串格式时间
- Java中时间显示格式
- java Date 显示格式
- Java Date函数的时间格式
- JAVA将十位时间戳格式化为日期Date类型
- JAVA中关于DATE时间日期加减、String与Date类型的转换
- java Date格式的string转换为Date类型
- Java Date 时间类型的操作
- 获取 yyyy-mm-dd 格式的 Date 时间类型
- String格式的时间转换为Date类型
- json格式的时间显示Date问题解决方法
- java中日期时间格式与毫秒数的转换如何将指定时间转换为Date类型
- java中如何将String类型的日期格式为yyyyMMdd转化为date类型的yyyy-MM-dd格式
- Java 时间格式 new Date()
- C++第五周mooc在线测评— 问题的模块化求解
- 告诉你应该选择 openSUSE 的五大理由
- 不要在该奋斗的年龄,选择安逸,选择逃避
- http://blog.csdn.net/lzwglory/article/details/49473013 spring中设计模式详解
- 每天一个常用的linux命令(5)--rm
- java 中Date类型的时间显示格式
- zip格式文件伪加密
- JSP指令
- JAVA7并发编程手册笔记
- 倒计时
- Python3.4使用pymysql连接MySQL
- 莫比乌斯反演 传送门
- NOIP2005提高组——等价表达式
- JQuery 绑定select标签的onchange事件,弹出选择的值,并实现跳转、传参