首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

相干日期的代码

2012-11-10 
有关日期的代码package com.lenovo.lps.epub.eu.utilimport java.text.ParseExceptionimport java.text.

有关日期的代码

package com.lenovo.lps.epub.eu.util;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;public class DateUtil {/** * 格式化指定日期 *  * @param date * @param exp * @return */public static String formatDate(Date date, String exp) {SimpleDateFormat simpleDateFormat = new SimpleDateFormat(exp);return simpleDateFormat.format(date);}/** *  * @author:meiyx * @date:2010-11-23 * @method description: 获得当前时间 * @return */public static Date getCurrentDate() {return new Date();}/** *  * @author:meiyx * @date:2010-11-23 * @method description:获得当前月 * @return */public static int getCurrentMonth() {return getCurrentDate().getMonth() + 1;}/** * @method description:获得当前年 * @author:meiyx * @date:2010-12-1 * @return */public static int getCurrentYear() {return getCurrentDate().getYear() + 1900;}/** * @method description:获得给定日期月的第一天 * @author:meiyx * @date:2010-12-1 * @param date */public static Date getMonthFirstDay(Date date) {Calendar calendar = Calendar.getInstance();calendar.setTime(date);calendar.set(Calendar.DAY_OF_MONTH, calendar.getMinimum(Calendar.DAY_OF_MONTH));return calendar.getTime();}/** * @method description:获得给定日期月的最后一天 * @author:meiyx * @date:2010-12-1 * @param date * @return */public static Date getMonthLastDay(Date date) {Calendar calendar = Calendar.getInstance();calendar.setTime(date);calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));return calendar.getTime();}/** *  * @author:meiyx 获得当前季节的开始时间 * @date:2010-11-24 * @method description: * @return * @throws ParseException * @throws Exception */public static Date getSeasonBeginTime() throws ParseException {int currentMonth = getCurrentMonth();SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");if (currentMonth >= 1 && currentMonth <= 3) {return dateFormat.parse(getCurrentYear() + "-01-01 00:00:00");} else if (currentMonth >= 4 && currentMonth <= 6) {return dateFormat.parse(getCurrentYear() + "-04-01 00:00:00");} else if (currentMonth >= 7 && currentMonth <= 9) {return dateFormat.parse(getCurrentYear() + "-07-01 00:00:00");} else {return dateFormat.parse(getCurrentYear() + "-10-01 00:00:00");}}/** *  * @author:meiyx * @date:2010-11-24 * @method description:获得当前季节的结束时间 * @return * @throws Exception */public static Date getSeasonEndTime() throws Exception {int currentMonth = getCurrentMonth();SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");if (currentMonth >= 1 && currentMonth <= 3) {return dateFormat.parse(getCurrentYear() + "-03-31 23:59:59");} else if (currentMonth >= 4 && currentMonth <= 6) {return dateFormat.parse(getCurrentYear() + "-06-30 23:59:59");} else if (currentMonth >= 7 && currentMonth <= 9) {return dateFormat.parse(getCurrentYear() + "-09-30 23:59:59");} else {return dateFormat.parse(getCurrentYear() + "-12-31 23:59:59");}}/** *  * @author:meiyx * @date:2010-11-24 * @method description:获得上季节的开始时间 * @return * @throws Exception */public static Date getPreSeasonBeginTime() throws ParseException {int currentMonth = getCurrentMonth();SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");if (currentMonth >= 1 && currentMonth <= 3) {return dateFormat.parse(getCurrentYear()-1 + "-10-01 00:00:00");} else if (currentMonth >= 4 && currentMonth <= 6) {return dateFormat.parse(getCurrentYear() + "-01-01 00:00:00");} else if (currentMonth >= 7 && currentMonth <= 9) {return dateFormat.parse(getCurrentYear() + "-04-01 00:00:00");} else {return dateFormat.parse(getCurrentYear() + "-07-01 00:00:00");}}/** *  * @author:meiyx * @date:2010-11-24 * @method description:获得上季节的结束时间 * @return * @throws Exception */public static Date getPreSeasonEndTime() throws Exception {int currentMonth = getCurrentMonth();SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");if (currentMonth >= 1 && currentMonth <= 3) {return dateFormat.parse(getCurrentYear()-1 + "-12-31 23:59:59");} else if (currentMonth >= 4 && currentMonth <= 6) {return dateFormat.parse(getCurrentYear() + "-03-31 23:59:59");} else if (currentMonth >= 7 && currentMonth <= 9) {return dateFormat.parse(getCurrentYear() + "-06-30 23:59:59");} else {return dateFormat.parse(getCurrentYear() + "-09-30 23:59:59");}}/** * 返回当前时间的毫秒数从1970 年 1 月 1日开始 *  * @return long */public static long getTimeInMillis() {long time = 0;// Calendar c = Calendar.getInstance();// time = c.getTimeInMillis();// time = new Date().getTime();time = System.currentTimeMillis();return time;}/** * 返回当前时间的字符串形式 : yyyy-MM-ddTHH:mm:ssZ *  * @return */public static String getTimestampForString() {return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").format(new Date());}/** * topList超时时间(一月中的毫秒数) */static Calendar calendar = Calendar.getInstance();public static final String topListCacheTimeMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH)* 24 * 60 * 60 * 1000 + "";/** * 获得当前时间的指定形式 *  * @param exp * @return */public static String getTimestamp(String exp) {SimpleDateFormat simpleDateFormat = new SimpleDateFormat(exp);return simpleDateFormat.format(new Date());}/** * PARSE指定格式的字符串日期 *  * @param strDate * @param exp * @return */public static Date parseDate(String strDate, String exp) {SimpleDateFormat simpleDateFormat = new SimpleDateFormat(exp);try {return simpleDateFormat.parse(strDate);} catch (ParseException e) {e.printStackTrace();return null;}}/** * 将java.util.Date 转为java.sql.Date *  * @param date * @return */public static java.sql.Date parseUtilDateToSqlDate(Date date) {return new java.sql.Date(date.getTime());}/** * 添加月份 * @param date */public static Date addMonth(Date date,int num){Calendar calendar=Calendar.getInstance();calendar.setTime(date);calendar.add(Calendar.MONDAY,num);return calendar.getTime();}/** * @method description:当前月的毫秒数 * @author:meiyx * @date:2010-12-23 * @return */   public static String getCurrentMonthMillisecond(){   Calendar calendar = Calendar.getInstance();return calendar.getActualMaximum(Calendar.DAY_OF_MONTH)*24*60*60*1000+"";   }}

热点排行