• Java 获取当前日期和时间


    Java 获取当前日期和时间

    import java.util.*; 
    import java.text.*;
    //以下默认时间日期显示方式都是汉语语言方式
    //一般语言就默认汉语就可以了,时间日期的格式默认为MEDIUM风格,比如:2008-6-16 20:54:53
    //以下显示的日期时间都是再Date类的基础上的来的,还可以利用Calendar类来实现见类TestDate2.java
    public class TestDate { 
       public static void main(String[] args) { 
          Date now = new Date(); 
          Calendar cal = Calendar.getInstance(); 
          
          DateFormat d1 = DateFormat.getDateInstance(); //默认语言(汉语)下的默认风格(MEDIUM风格,比如:2008-6-16 20:54:53)
          String str1 = d1.format(now);
          DateFormat d2 = DateFormat.getDateTimeInstance(); 
          String str2 = d2.format(now); 
          DateFormat d3 = DateFormat.getTimeInstance(); 
          String str3 = d3.format(now); 
          DateFormat d4 = DateFormat.getInstance(); //使用SHORT风格显示日期和时间
          String str4 = d4.format(now);

          DateFormat d5 = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL); //显示日期,周,时间(精确到秒)
          String str5 = d5.format(now);
          DateFormat d6 = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG); //显示日期。时间(精确到秒)
          String str6 = d6.format(now);
          DateFormat d7 = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT); //显示日期,时间(精确到分)
          String str7 = d7.format(now);
          DateFormat d8 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM); //显示日期,时间(精确到分)
          String str8 = d8.format(now);//与SHORT风格相比,这种方式最好用


          
          System.out.println("用Date方式显示时间: " + now);//此方法显示的结果和Calendar.getInstance().getTime()一样
          
          
          System.out.println("用DateFormat.getDateInstance()格式化时间后为:" + str1);
          System.out.println("用DateFormat.getDateTimeInstance()格式化时间后为:" + str2);
          System.out.println("用DateFormat.getTimeInstance()格式化时间后为:" + str3);
          System.out.println("用DateFormat.getInstance()格式化时间后为:" + str4);
          
          System.out.println("用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化时间后为:" + str5);
          System.out.println("用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化时间后为:" + str6);
          System.out.println("用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化时间后为:" + str7);
          System.out.println("用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化时间后为:" + str8);
       }

    }

    运行结果:

    用Date方式显示时间: Mon Jun 16 20:54:53 CST 2008
    用DateFormat.getDateInstance()格式化时间后为:2008-6-16
    用DateFormat.getDateTimeInstance()格式化时间后为:2008-6-16 20:54:53
    用DateFormat.getTimeInstance()格式化时间后为:20:54:53
    用DateFormat.getInstance()格式化时间后为:08-6-16 下午8:54
    用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化时间后为
    :2008年6月16日 星期一 下午08时54分53秒 CST
    用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化时间后为
    :2008年6月16日 下午08时54分53秒
    用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化时间后
    为:08-6-16 下午8:54
    用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化时间
    后为:2008-6-16 20:54:53

    摘自:http://www.blogjava.net/parable-myth/archive/2013/01/17/394364.html

  • 相关阅读:
    linux内核中的subsys_initcall是干什么的?
    linux内核中的MFD子系统
    linux内核中有哪些子系统(框架)呢?
    软件架构师书籍
    求最大公约数和最小公倍数
    写一个函数判断字符串中"{"与"}","["与"]","("与")"匹配,"{"必须在"}"前面,"["必须在"]"前面,"("必须在")"前面,可以嵌套
    请用程序写出冒泡排序算法,并做相应改进使得排序效率更高
    50个必备的实用jQuery代码段+ 可以直接拿来用的15个jQuery代码片段
    js同比例缩放图片
    oracle 10g函数大全--其他函数
  • 原文地址:https://www.cnblogs.com/SangBigYe/p/3546470.html
Copyright © 2020-2023  润新知