• 时间简码-java


    java里的时间类是比较简单的,如果没有对时间相关类总结学习过,有些需求或许不能立即写出来。现做几个简单的时间编码方法,供以后参考查阅。

     1 //获取距离当天前三个月的日期
     2 {
     3     Calendar calendar = Calendar.getInstance();
     4     calendar.add(Calendar.MONTH, -3);
     5     SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");        
     6     String predate = format.format(calendar.getTime());
     7     Date now = new Date();
     8     SimpleDateFormat nformat = new SimpleDateFormat("yyyyMMdd");
     9     String nowdate = nformat.format(now);
    10     System.out.println(predate+";"+nowdate);
    11 }
    输出:
    20130826;20131126
    1 //日期date是星期几
    2 int getWeekNum(Date date){
    3     Calendar calendar = Calendar.getInstance();
    4     calendar.setTime(date);
    5     int num = calendar.get(Calendar.DAY_OF_WEEK);
    6     return num==1?7:num-1;
    7 }
    输出:
    星期一:1
    星期二:2
    星期三:3
    星期四:4
    星期五:5
    星期六:6
    星期日:7
      1 //判断一年是否为闰年
      2 static boolean IsLeapYear(int year){
      3     return (year % 400 == 0 || year % 4 == 0 && year % 100 != 0);
      4 }
      5 //获得某一年的总天数
      6 static int GetAllDays(int year){
      7     return (IsLeapYear(year)? 366: 365);
      8 }
      9 //获得某年、某月的最大天数
     10 static int GetMaxDay(int year,int month){
     11     switch(month)
     12     {
     13     case 1:
     14     case 3:
     15     case 5:
     16     case 7:
     17     case 8:
     18     case 10:
     19     case 12:
     20                 return 31;        
     21     case 4:
     22     case 6:
     23     case 9:
     24     case 11:
     25         return 30;    
     26     case 2:
     27         return (IsLeapYear(year)? 29: 28);    
     28     default:
     29         return -1;
     30     }
     31 }
     32 //获得某年、某月、某日是这一年的第几天
     33 static int GetDays(int year,int month,int day){
     34     int sum = 0;
     35     
     36     for(int i = 1; i < month ; i++){
     37         sum += GetMaxDay(year,i);
     38     }
     39     
     40     return sum+day;
     41 }
     42 //获得某年某月某日的下一天
     43 static void GetNextDay(int year,int month,int day){
     44     if(day != GetMaxDay(year,month)){
     45         day++;
     46     }else
     47     {
     48         if(month != 12)
     49         {month++; day = 1;}
     50         else
     51         {year++; month = day =1;}
     52     }
     53 }
     54 //获得某年某月某日的前一天
     55 static void GetPreDay(int year,int month,int day){
     56     if(day != 1){
     57         day--;
     58     }else{
     59         if(month != 1){
     60             month--;
     61             GetMaxDay(year,month);
     62         }
     63         else{
     64             year--;month = 12;day = 31;
     65         }
     66     }
     67 }
     68 //获得某年某月某日n天之后的日期
     69 static void GetXDay(int year,int month,int day,int X){
     70     for(int i = 1; i<= X; i++){
     71         if(day != GetMaxDay(year,month)){
     72             day++;
     73         }else{
     74             if(month != 12){
     75                 month++;day = 1;
     76             }else{
     77                 year++;
     78                 month = day = 1;
     79             }
     80         }
     81     }
     82     System.out.println(X+" 天后是:"+year+"-"+month+"-"+day);
     83     
     84         
     85 }
     86 //比较日期大小
     87 static int CompareDay(int year1,int month1,int day1, int year2,int month2,int day2){
     88     return year1 != year2? (year1-year2):(month1 != month2 ? (month1-month2): day1-day2);
     89 }
     90 //求日期之差
     91 static int GetDateDiff1(int year1,int month1,int day1,int year2,int month2,int day2){
     92     int sum = GetAllDays(year1) - GetDays(year1,month1,day1);
     93     for(int i = year1+1; i < year2; i++ ){
     94         sum += GetAllDays(i);
     95     }
     96     sum += GetDays(year2,month2,day2);
     97     return sum;
     98 }
     99 static int GetDateDiff(int year1,int month1,int day1,int year2,int month2,int day2){
    100         if(CompareDay(year1,month1,day1,year2,month2,day2) > 0){
    101         return -GetDateDiff1(year2,month2,day2,year1,month1,day1);
    102     }else if(CompareDay(year1,month1,day1,year2,month2,day2) < 0){
    103         return GetDateDiff1(year1,month1,day1,year2,month2,day2);
    104     }
    105     return 0;
    106 }        
     1 //指定时间距离当前时间
     2 String getAwayTime(long time){
     3     Calendar calendar = Calendar.getInstance();
     4     long awaytime = calendar.getTimeInMillis()-time;
     5     if(awaytime/1000 < 60){
     6         return "不到1分钟!";
     7     }
     8     if(awaytime/1000/60 < 60){
     9         return awaytime/1000/60+"分钟前!";
    10     }
    11     if(awaytime/1000/60/60 < 24){
    12         return awaytime/1000/60/60+"小时前!";
    13     }
    14     return awaytime/1000/60/60/24+"天前!";
    15 }
        /**
         * 获取当前日期 
         * 如 20120412
         * 
         */
        public static String getCurrDate(){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            return sdf.format(new Date());
        }
        /**
         * 获取当前年份 
         * 如 2012
         * 
         */
        public static String getCurrDate(){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
            return sdf.format(new Date());
        }
  • 相关阅读:
    如何设置body高度为浏览器高度
    h5的video下载按钮如何隐藏
    微信小程序中的子父组件传值问题
    elementUI级联选择器2(选择及回显)编辑保存
    elementUI级联选择器(选择及回显)
    vue+elementUI 表格操作行的增删改查
    单独验证非form表单中的input(限制)
    JS中去除数组中的假值(0, 空,undefined, null, false)
    vue 组件之间的传值 (父子传值、兄弟传值)
    http协议的状态码
  • 原文地址:https://www.cnblogs.com/mayt-/p/3443413.html
Copyright © 2020-2023  润新知