zoukankan      html  css  js  c++  java
  • JAVA Date类

    package com.feicuiedu.test;

    import java.text.DateFormat;
    import java.util.Calendar;
    import java.util.Date;


    public class date {
    public void getTimeByDate(){
    Date date = new Date();
    DateFormat df1 = DateFormat.getDateInstance();//日期格式,精确到日
    System.out.println(df1.format(date));
    DateFormat df2 = DateFormat.getDateTimeInstance();//可以精确到时分秒
    System.out.println(df2.format(date));
    DateFormat df3 = DateFormat.getTimeInstance();//只显示出时分秒
    System.out.println(df3.format(date));
    DateFormat df4 = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL); //显示日期,周,上下午,时间(精确到秒)
    System.out.println(df4.format(date));
    DateFormat df5 = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG); //显示日期,上下午,时间(精确到秒)
    System.out.println(df5.format(date));
    DateFormat df6 = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT); //显示日期,上下午,时间(精确到分)
    System.out.println(df6.format(date));
    DateFormat df7 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM); //显示日期,时间(精确到分)
    System.out.println(df7.format(date));
    }
    public void getTimeByCalendar(){
    Calendar cal = Calendar.getInstance();
    int year = cal.get(Calendar.YEAR);//获取年份
    int month=cal.get(Calendar.MONTH);//获取月份
    int day=cal.get(Calendar.DATE);//获取日
    int hour=cal.get(Calendar.HOUR);//小时
    int minute=cal.get(Calendar.MINUTE);//分
    int second=cal.get(Calendar.SECOND);//秒
    int WeekOfYear = cal.get(Calendar.DAY_OF_WEEK);//一周的第几天
    System.out.println("现在的时间是:公元"+year+"年"+month+"月"+day+"日 "+hour+"时"+minute+"分"+second+"秒 星期"+WeekOfYear);
    }
    public static void main(String[] args) {
    date t=new date();
    t.getTimeByDate();
    System.out.println("****************************");
    t.getTimeByCalendar();
    }
    }

  • 相关阅读:
    新内核2.6.30编译完之后在目标板上看不到ttyS1
    使用memset、memcpy等函数需要包含string.h而不是strings.h
    软件模式之原则设计
    由编译错误看L. lxxxx的正确位置
    设计模式之策略模式
    make menuconfig提示'make menuconfig' requires the ncurses libraries.
    抽取界面用 XML 和 XSL 构建有良好适应性的 Web 应用前端
    .Net框架下的XSLT转换技术简介
    派生和继承
    UML 类图介绍
  • 原文地址:https://www.cnblogs.com/dhm520/p/8524615.html
Copyright © 2011-2022 走看看