常用的获取当前日期的方法,在日期校验时很有用:
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 }
时间: 2024-10-07 05:29:43