java 日期时间处理类
2018-07-20 来源:open-open
import java.util.Calendar; import java.sql.Date; import java.text.SimpleDateFormat; import java.text.ParseException; /** * Utility to date time * @version 0.1 * @author Super Zhao */ public class DateTimeUtil { /** * 由java.util.Date到java.sql.Date的类型转换 * @param date * @return Date */ public static Date getSqlDate (java.util.Date date) { return new Date (date.getTime() ); } public static Date nowDate() { Calendar calendar = Calendar.getInstance(); return getSqlDate (calendar.getTime() ); } /** * 获得某一日期的后一天 * @param date * @return Date */ public static Date getNextDate (Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime (date); int day = calendar.get (Calendar.DATE); calendar.set (Calendar.DATE, day + 1); return getSqlDate (calendar.getTime() ); } /** * 获得某一日期的前一天 * @param date * @return Date */ public static Date getPreviousDate (Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime (date); int day = calendar.get (Calendar.DATE); calendar.set (Calendar.DATE, day - 1); return getSqlDate (calendar.getTime() ); } /** * 获得某年某月第一天的日期 * @param year * @param month * @return Date */ public static Date getFirstDayOfMonth (int year, int month) { Calendar calendar = Calendar.getInstance(); calendar.set (Calendar.YEAR, year); calendar.set (Calendar.MONTH, month - 1); calendar.set (Calendar.DATE, 1); return getSqlDate (calendar.getTime() ); } /** * 获得某年某月最后一天的日期 * @param year * @param month * @return Date */ public static Date getLastDayOfMonth (int year, int month) { Calendar calendar = Calendar.getInstance(); calendar.set (Calendar.YEAR, year); calendar.set (Calendar.MONTH, month); calendar.set (Calendar.DATE, 1); return getPreviousDate (getSqlDate (calendar.getTime() ) ); } /** * 由年月日构建java.sql.Date类型 * @param year * @param month * @param date * @return Date */ public static Date buildDate (int year, int month, int date) { Calendar calendar = Calendar.getInstance(); calendar.set (year, month - 1, date); return getSqlDate (calendar.getTime() ); } /** * 取得某月的天数 * @param year * @param month * @return int */ public static int getDayCountOfMonth (int year, int month) { Calendar calendar = Calendar.getInstance(); calendar.set (Calendar.YEAR, year); calendar.set (Calendar.MONTH, month); calendar.set (Calendar.DATE, 0); return calendar.get (Calendar.DATE); } /** * 获得某年某季度的最后一天的日期 * @param year * @param quarter * @return Date */ public static Date getLastDayOfQuarter (int year, int quarter) { int month = 0; if (quarter > 4) { return null; } else { month = quarter * 3; } return getLastDayOfMonth (year, month); } /** * 获得某年某季度的第一天的日期 * @param year * @param quarter * @return Date */ public static Date getFirstDayOfQuarter (int year, int quarter) { int month = 0; if (quarter > 4) { return null; } else { month = (quarter - 1) * 3 + 1; } return getFirstDayOfMonth (year, month); } /** * 获得某年的第一天的日期 * @param year * @return Date */ public static Date getFirstDayOfYear (int year) { return getFirstDayOfMonth (year, 1); } /** * 获得某年的最后一天的日期 * @param year * @return Date */ public static Date getLastDayOfYear (int year) { return getLastDayOfMonth (year, 12); } /** * String到java.sql.Date的类型转换 * @param param * @return Date */ public static java.sql.Date StringToDate (String param) { if (StringUtil.nullOrBlank (param) ) { return null; } else { java.util.Date date = null; try { SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd"); date = sdf.parse (param); return new Date (date.getTime() ); } catch (ParseException ex) { //ex.printStackTrace(); return null; } } } }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
最新资讯
热门推荐