多语言展示
当前在线:583今日阅读:155今日分享:35

java中时间的工具类

简单的时间工具类
方法/步骤
1

public abstract class DateUtils { private static SimpleDateFormat dayFormatter = new SimpleDateFormat('yyyy-MM-dd'); private DateUtils() {  } /** * 获取当前年 */ public static int getCurrentYear() { Calendar calendar = Calendar.getInstance(); calendar.get(Calendar.YEAR); return calendar.get(Calendar.YEAR); }

2

获取当前月份:public static int getCurrentMonth() { Calendar calendar = Calendar.getInstance(); return calendar.get(Calendar.MONTH) + 1; }

3

获取所有月份:public static List getMonths(){ List  months = new ArrayList(); int nowMonth = getCurrentMonth(); for(int i = 1;i <= nowMonth;i++){ months.add(i); } return months; }

4

获取季度:public static Map getQuarter(){ Map  months = new HashMap(); int nowMonth = getCurrentMonth(); if(nowMonth >= 12){ months.put(13,'一季度'); months.put(14,'二季度'); months.put(15,'三季度'); months.put(16,'四季度'); months.put(17,'上半年'); months.put(18,'下半年'); months.put(19,'全年'); }

5

else if(nowMonth >= 9){months.put(13,'一季度');months.put(14,'二季度');months.put(15,'三季度');months.put(17,'上半年');}else if(nowMonth >= 6){months.put(13,'一季度');months.put(14,'二季度');months.put(17,'上半年');}

6

else if(nowMonth >= 3){months.put(13,'一季度');}return months;}

7

String类型的时间字符串转时间:public static Date stringToDate(String date, String partten) throws Exception{  if(StringUtils.isBlank(date) ) throw new Exception('date is null'); if(StringUtils.isBlank(partten)) return dayFormatter.parse(date); else return new SimpleDateFormat(partten).parse(date); }

推荐信息