码迷,mamicode.com
首页 > 其他好文 > 详细

获取当前日期小方法(可用于日期校验)

时间:2015-05-14 11:24:29      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:

常用的获取当前日期的方法,在日期校验时很有用:

 1 //Get Current Date Method for EntryDate Check
 2     public static String getCurrentDate() {
 3             
 4         long millsecs = System.currentTimeMillis();
 5         long secs = millsecs / 1000;
 6         long mins = secs / 60;
 7         int hours = (int)(mins / 60);
 8         int days = hours / 24;
 9         int fourYears = days / 1461;
10         int elseDays = days % 1461;
11         int elseYears = elseDays / 365;
12         int remainDays = elseDays % 365 + 1;
13         int year = fourYears * 4 + elseYears + 1970;
14             
15         int month = 1;
16         int wholeMonthDays = 0;
17         int monthCount = 0;
18         for(;wholeMonthDays < remainDays;month++) {
19             wholeMonthDays += getMonthDays(year, month);
20             monthCount++;
21         }
22         wholeMonthDays -= getMonthDays(year, --month);    
23         int lastDays = remainDays - wholeMonthDays;
24             
25         String currentDate = String.format("%d-%d-%d", year , monthCount , lastDays);
26         return currentDate;
27     }
28 public static int getMonthDays(int year , int month) { 29 int monthdays = 0; 30 switch (month) { 31 case 1: 32 case 3: 33 case 5: 34 case 7: 35 case 8: case 10: 36 case 12: 37 monthdays = 31; 38 break; 39 case 4: 40 case 6: 41 case 9: 42 case 11: 43 monthdays = 30; 44 break; 45 case 2: 46 if(year / 4 == 0) 47 monthdays = 29; 48 else 49 monthdays = 28; 50 break; 51 } 52 return monthdays; 53 }

 

获取当前日期小方法(可用于日期校验)

标签:

原文地址:http://www.cnblogs.com/Oh-Bug/p/4502579.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!