java Date类型的时间显示格式

创建一个日期对象

使用系统的当前日期和时间创建一个日期对象并返回一个长整数的简单例子。 这个时间通常被称为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制的时间

 

原文地址:https://www.cnblogs.com/ytc6/p/8469469.html