zoukankan      html  css  js  c++  java
  • 日期类的使用

     1 package cn.zhang.test;
     2 
     3 import java.util.Calendar;
     4 import java.util.Date;
     5 import java.util.GregorianCalendar;
     6 
     7 /**
     8  * 测试日期类的使用
     9  * @author 张涛
    10  *
    11  */
    12 public class TestCalendar {
    13     public static void main(String[] args) {
    14         
    15         /*获得日期相关的元素*/
    16         Calendar c1 = new GregorianCalendar(2019,8,20,16,30,00);
    17         int year = c1.get(Calendar.YEAR);
    18         int month = c1.get(Calendar.MONTH);
    19         int day = c1.get(Calendar.DATE);//也可以使用DAY_OF_MONTH
    20         int weekday = c1.get(Calendar.DAY_OF_WEEK);//星期几
    21         System.out.println(year);
    22         System.out.println(month);
    23         System.out.println(day);
    24         System.out.println("周" + weekday);
    25         printCalendar(c1);
    26         
    27         /*设置日期相关的元素*/
    28         Calendar c2 = new GregorianCalendar();//默认获得当前日期
    29         System.out.println(c2);
    30         c2.set(Calendar.YEAR, 2020);
    31         System.out.println(c2);
    32         
    33         /*日期的计算*/
    34         Calendar c3 = new GregorianCalendar();
    35         c3.add(Calendar.YEAR, 100);
    36         System.out.println(c3);
    37         
    38         /*日期对象和时间对象的转化*/
    39         Calendar c4 = new GregorianCalendar();
    40         Date d = c4.getTime();//日期对象转时间对象
    41         System.out.println(d);
    42         
    43         Calendar c5 = new GregorianCalendar();
    44         c5.setTime(new Date());
    45         System.out.println(c5);
    46     }
    47     
    48     public static void printCalendar(Calendar c) {
    49         //打印时间 格式xxxx年xx月xx日xx时xx分xx秒
    50         int year = c.get(Calendar.YEAR);
    51         int month = c.get(Calendar.MONTH)+1;
    52         int date = c.get(Calendar.DATE);
    53         int week = c.get(Calendar.DAY_OF_WEEK)-1;
    54         String weekday = week == 0?"日":week+"";
    55         int hour = c.get(Calendar.HOUR);
    56         int minute = c.get(Calendar.MINUTE);
    57         int second = c.get(Calendar.SECOND);
    58         
    59         System.out.println(year+"年"+month+"月"+date+"日"+hour+"时"+minute+"分"+second+"秒"+" 周"+ weekday);
    60     }
    61     
    62     
    63     
    64     
    65     
    66     
    67     
    68     
    69     
    70     
    71     
    72     
    73     
    74     
    75     
    76     
    77     
    78     
    79     
    80     
    81     
    82     
    83     
    84     
    85 }
  • 相关阅读:
    ios开源项目2
    Cocoa 框架 For iOS(一) 框架的介绍
    iPhone开源项目大全
    8款iOS的日历开源代码
    二维码扫描工具和开发包 ZBar
    图文解释XCode常用快捷键的使用
    常用的iOS高效开源类库
    static_cast
    [转]SecureCRT rz 上传文件失败问题
    Linux中的EAGAIN含义
  • 原文地址:https://www.cnblogs.com/zhangqiling/p/11384089.html
Copyright © 2011-2022 走看看