对比新文件 |
| | |
| | | package com.gkhy.safePlatform.emergency.utils; |
| | | |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Calendar; |
| | | import java.util.Date; |
| | | |
| | | public class TimeUtils { |
| | | |
| | | public static String formatDate(Date date){ |
| | | SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd"); |
| | | String sDate = f.format(date); |
| | | return sDate; |
| | | } |
| | | |
| | | /** |
| | | * 获取某年第一天日期 |
| | | * @param year 年份 |
| | | * @return Date |
| | | */ |
| | | public static String getYearFirst(int year){ |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.clear(); |
| | | calendar.set(Calendar.YEAR, year); |
| | | Date currYearFirst = calendar.getTime(); |
| | | return formatDate(currYearFirst); |
| | | } |
| | | |
| | | /** |
| | | * 获取某年最后一天日期 |
| | | * @param year 年份 |
| | | * @return Date |
| | | */ |
| | | public static String getYearLast(int year){ |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.clear(); |
| | | calendar.set(Calendar.YEAR, year); |
| | | calendar.roll(Calendar.DAY_OF_YEAR, -1); |
| | | Date currYearLast = calendar.getTime(); |
| | | |
| | | return formatDate(currYearLast); |
| | | } |
| | | |
| | | /** |
| | | * 获取某月第一天日期 |
| | | * @param month 年份 |
| | | * @return Date |
| | | */ |
| | | public static String getMonthFirst(int year ,int month){ |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.clear(); |
| | | calendar.set(Calendar.YEAR, year); |
| | | calendar.set(Calendar.MONTH, month-1); |
| | | Date currYearFirst = calendar.getTime(); |
| | | return formatDate(currYearFirst); |
| | | } |
| | | |
| | | /** |
| | | * 获取某月最后一天日期 |
| | | * @param month 年份 |
| | | * @return Date |
| | | */ |
| | | public static String getMonthLast(int year ,int month){ |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.clear(); |
| | | calendar.set(Calendar.YEAR, year); |
| | | calendar.set(Calendar.MONTH, month-1); |
| | | calendar.roll(Calendar.DAY_OF_MONTH, -1); |
| | | Date currYearLast = calendar.getTime(); |
| | | |
| | | return formatDate(currYearLast); |
| | | } |
| | | |
| | | |
| | | public static void main(String[] args) { |
| | | Calendar date = Calendar.getInstance(); |
| | | int year = date.get(Calendar.YEAR); |
| | | int month = date.get(Calendar.MONTH); |
| | | System.out.println(getMonthLast(year,2)); |
| | | } |
| | | } |