zoukankan      html  css  js  c++  java
  • Java 日期 Api

     1 public class TimeTest {
     2 
     3     public static void main(String[] args) {
     4         Date d1 = new Date();
     5         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd:HH-mm-ss");
     6         System.out.println(sdf.format(d1));
     7 
     8         Calendar c1 = Calendar.getInstance();
     9         int year = c1.get(Calendar.YEAR);
    10         int month = c1.get(Calendar.MONTH) + 1;
    11         int day = c1.get(Calendar.DAY_OF_MONTH);
    12         int hour = c1.get(Calendar.HOUR);
    13         int minute = c1.get(Calendar.MINUTE);
    14         int second = c1.get(Calendar.SECOND);
    15         System.out.println(year + "年" + month + "月" + day + "日" + hour + "时" + minute + "分" + second + "秒");
    16 
    17         int hourOfDay = c1.get(Calendar.HOUR_OF_DAY);
    18         System.out.println("一天的第" + hourOfDay + "小时");
    19 
    20         int dayOfWeek = c1.get(Calendar.DAY_OF_WEEK);
    21         System.out.println("一周的第" + dayOfWeek + "天");
    22 
    23         int dayOfMonth = c1.get(Calendar.DAY_OF_MONTH);
    24         System.out.println("一月的第" + dayOfMonth + "天");
    25 
    26         int dayOfYear = c1.get(Calendar.DAY_OF_YEAR);
    27         System.out.println("一年的第" + dayOfYear + "天");
    28 
    29         int weekOfYear = c1.get(Calendar.WEEK_OF_YEAR);
    30         System.out.println("一年的第" + weekOfYear + "周");
    31 
    32         int weeksInWeekYear = c1.getWeeksInWeekYear();
    33         System.out.println("一年有" + weeksInWeekYear + "周");
    34 
    35         Date time = c1.getTime();
    36         System.out.println("Date:" + time);
    37 
    38         TimeZone timeZone = c1.getTimeZone();
    39         System.out.println("时区:" + timeZone);
    40 
    41         long timeInMillis = c1.getTimeInMillis();
    42         System.out.println(timeInMillis);
    43 
    44 
    45         int weekYear = c1.getWeekYear();
    46         System.out.println("今年是:" + weekYear);
    47 
    48     }
    49 }

    测试结果:

    2016-09-06:14-41-16
    2016年9月6日2时41分16秒
    一天的第14小时
    一周的第3天
    一月的第6天
    一年的第250天
    一年的第37周
    一年有53周
    Date:Tue Sep 06 14:41:16 CST 2016
    时区:sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null]
    1473144076561
    今年是:2016

    Java 中日期有2个类

    1. Date + SimpleDateFormat

    Date 类最主要的作用就是获得当前时间,同时这个类里面也具有设置时间以及一些其他的功能,但是由于本身设计的问题,这些方法却遭到众多批评,不建议使用,更推荐使用 Calendar 类进行时间和日期的处理

    2. Calender

    在代码中遇到日期处理,建议使用该类,通过 getInstance() 方法获取到实例。

    参考:http://www.apihome.cn/api/java/Calendar.html

  • 相关阅读:
    python note 30 断点续传
    python note 29 线程创建
    python note 28 socketserver
    python note 27 粘包
    python note 26 socket
    python note 25 约束
    Sed 用法
    python note 24 反射
    python note 23 组合
    python note 22 面向对象成员
  • 原文地址:https://www.cnblogs.com/liyiran/p/5845694.html
Copyright © 2011-2022 走看看