• Java Calendar获取年、月、日、时间


     1 Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));    //获取东八区时间
     2 
     3   int year = c.get(Calendar.YEAR);    //获取年
     4   int month = c.get(Calendar.MONTH) + 1;   //获取月份,0表示1月份
     5   int day = c.get(Calendar.DAY_OF_MONTH);    //获取当前天数
     6   int first = c.getActualMinimum(c.DAY_OF_MONTH);    //获取本月最小天数
     7   int last = c.getActualMaximum(c.DAY_OF_MONTH);    //获取本月最大天数
     8   int time = c.get(Calendar.HOUR_OF_DAY);       //获取当前小时
     9   int min = c.get(Calendar.MINUTE);          //获取当前分钟
    10   int xx = c.get(Calendar.SECOND);          //获取当前秒
    11 
    12  
    13 
    14   SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    15 
    16   String curDate = s.format(c.getTime());  //当前日期
    17   System.out.println("当前时间:"+year + "-" + month + "-"+ day + " "+time + ":" + min +":" + xx);
    18   System.out.println("第一天和最后天:" + first +"," + last);
    19 
    20   System.out.println("当前日期curDate====:" + curDate);
    21 
    22  
    23 
    24 输出结果:
    25 
    26 当前时间:2012-9-25 22:50:54
    27 第一天和最后天:1,30
    28 
    29 当前日期curDate:2012-09-25 22:50:54
    30 
    31  
    32 
    33 Calendar的计算
    34 
    35   c.add(Calendar.YEAR, 1);
    36   c.add(Calendar.MONTH, 1);
    37   c.add(Calendar.DAY_OF_MONTH, 1);
    38   int year2 = c.get(Calendar.YEAR);
    39   int month2 = c.get(Calendar.MONTH) + 1;
    40   int day2 = c.get(Calendar.DAY_OF_MONTH);
    41   int firstD = c.getActualMinimum(c.DAY_OF_MONTH);
    42   int lastD = c.getActualMaximum(c.DAY_OF_MONTH);
    43   System.out.println("当前时间:"+year2 + "-" + month2 + "-"+ day2 + " "+time + ":" + min +":" + xx);
    44   System.out.println("第一天和最后天:" + firstD +"," + lastD);
    45 
    46  
    47 
    48 输出结果:
    49 
    50 当前时间:2013-10-26 23:4:3
    51 第一天和最后天:1,31
    52 
    53 
    54 
    55 获取上个月的年、月、日
    56 
    57 Calendar c=Calendar.getInstance();
    58 
    59 c.add(Calendar.MONTH, -1);//上个月
    60 
    61 SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    62 
    63 String year=String.valueOf(c.get(Calendar.YEAR));
    64 
    65 String topDay = String.valueOf(c.getActualMinimum(Calendar.DAY_OF_MONTH));
    66 
    67 String lastDay = String.valueOf(c.getActualMaximum(Calendar.DAY_OF_MONTH)); 
    68 
    69 //上个月
    70 
    71 String lastMonth=String.valueOf(c.get(Calendar.MONTH)+1).length()==2?String.valueOf(c.get(Calendar.MONTH)+1):"0"+String.valueOf(c.get(Calendar.MONTH)+1);
    72 
    73 String topDayMonth=year+"-"+lastMonth+"-"+"01" + " 00:00:00";
    74 
    75 String lastDayMonth = year+"-"+lastMonth+"-"+lastDay+ " 23:59:59";
    76 
    77 System.out.println("###year:" + year);
    78 
    79 System.out.println("###last month:" + lastMonth);
    80 
    81         System.out.println("###topday:" + topDayMonth);
    82 
    83         System.out.println("###lastday:" + lastDayMonth);
    84 
    85 
    86 
    87 输出结果
    88 
    89 ###year:2013
    90 
    91 ###last month:04
    92 
    93 ###topday:2013-04-01 00:00:00
    94 
    95 ###lastday:2013-04-30 23:59:59
    96 
    97 转自 http://hi.baidu.com/zzm_2007/item/3b967b1cd1586e4e2b3e2258
  • 相关阅读:
    *pwn*练习1——攻防世界-when_did_you_born
    PWN——uaf漏洞学习
    pwnable.kr之passcode
    pwnable.kr之flag
    pwnable.kr之brainf*ck
    pwnable.kr之simple Login
    pwnable.tw start&orw
    RTF文件格式
    jarvis level6_x64堆溢出unlink拾遗
    jarvisoj level5爬坑
  • 原文地址:https://www.cnblogs.com/stsinghua/p/6418768.html
Copyright © 2020-2023  润新知