packagecom.date; importjava.text.SimpleDateFormat; importjava.util.Calendar; importjava.util.Date; public classTest { public static SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); public static SimpleDateFormat format1 = newSimpleDateFormat( "yyyyMMdd HH:mm:ss"); /*** 得到指定日期的一天的的最后时刻23:59:59 * * @paramdate * @return */ public staticDate getFinallyDate(Date date) { String temp =format.format(date); temp += " 23:59:59"; try{ returnformat1.parse(temp); } catch(Exception e) { return null; } } /*** 得到指定日期的一天的开始时刻00:00:00 * * @paramdate * @return */ public staticDate getStartDate(Date date) { String temp =format.format(date); temp += " 00:00:00"; try{ returnformat1.parse(temp); } catch(Exception e) { return null; } } /*** 获取某年第一天日期开始时刻 * @paramyear 年份 * @returnDate */ public static Date getYearFirstDay(intyear){ Calendar cal =Calendar.getInstance(); cal.clear(); cal.set(Calendar.YEAR, year); Date yearFirstDay =cal.getTime(); returngetStartDate(yearFirstDay); } /*** 获取某年最后一天日期最后时刻 * @paramyear 年份 * @returnDate */ public static Date getYearLastDay(intyear){ Calendar cal =Calendar.getInstance(); cal.clear(); cal.set(Calendar.YEAR, year); cal.roll(Calendar.DAY_OF_YEAR, -1); Date yearLastDay =cal.getTime(); returngetFinallyDate(yearLastDay); } public static voidmain(String[] args) { Calendar cal =Calendar.getInstance(); //获取当前年份 int year =cal.get(Calendar.YEAR); System.out.println(year); System.out.println(getYearFirstDay(year)); System.out.println(getYearLastDay(year)); } }
JAVA获取某年(当年)的第一天的开始时刻和某年(当年)的最后一天的最后时刻
免责声明:文章转载自《JAVA获取某年(当年)的第一天的开始时刻和某年(当年)的最后一天的最后时刻》仅用于学习参考。如对内容有疑问,请及时联系本站处理。
上篇API Monitor简介(API监控工具)Mac usr/bin 目录 权限问题下篇
宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=