Java tutorial
/** * B5mDateUtils.java * * ? * ??B5mDateUtils * * ver ? ? ? * * V1.00 '12-05-24 iZENEsoft wiley.wang ? * * Copyright (c) 2009 iZENEsoft Business Software corporation All Rights Reserved. * LICENSE INFORMATION */ package com.b5m.user.frame.util; import org.apache.commons.lang.StringUtils; import java.io.Serializable; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * ? * @author Wiley * @version v1.0 */ public class DateUtils implements Serializable { private static final long serialVersionUID = 6622278926579307357L; public static final String DATE_STR_FORMATTER = "yyyy-MM-dd"; public static final int DAY_SECONDS = 24 * 60 * 60; public static String getToday() { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");//ComConstants.SDF_YYYYMMDDHHMMSS; return sdf.format(new Date()); } public static String getDateTime() { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssMs");//ComConstants.SDF_YYYYMMDDHHMMSS; return sdf.format(new Date()); } public static String getDateTime2() { SimpleDateFormat sdf = new SimpleDateFormat("MMddHHmmssMs");//ComConstants.SDF_YYYYMMDDHHMMSS; return sdf.format(new Date()); } public static synchronized String getInviteCodeByNowDate() { SimpleDateFormat sdf = new SimpleDateFormat("MMddHHmmssMs");//ComConstants.SDF_YYYYMMDDHHMMSS; String dateStr = sdf.format(new Date()); return getRandomInteger() + dateStr; } public static String getRandomInteger() { int temp = (int) (Math.random() * 9999); return String.valueOf(temp); } public static Date String2Date(String dateStr) { if (StringUtils.isBlank(dateStr)) { return null; } return String2Date(dateStr, "yyyyMMdd"); } public static String Date2String(Date date) { if (null == date) { return ""; } return Date2String(date, "yyyy-MM-dd"); } public static Date String2Date(String dateStr, String DateFormat) { SimpleDateFormat sdf = new SimpleDateFormat(DateFormat); try { return sdf.parse(dateStr); } catch (ParseException e) { e.printStackTrace(); return null; } } public static String Date2String(Date date, String DateFormat) { SimpleDateFormat sdf = new SimpleDateFormat(DateFormat); try { return sdf.format(date); } catch (Exception e) { e.printStackTrace(); return null; } } /** * ? * @param deadline ,?yyyyMMddHHmmss * @return */ public static String getRemainingTime(String endDate) { String rtn = ""; if (StringUtils.isBlank(endDate)) return rtn; SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");//ComConstants.SDF_YYYYMMDDHHMMSS;//??? try { Date deadline = sdf.parse(endDate); long remaining = deadline.getTime() - System.currentTimeMillis(); //long remaining=lngDeadline-System.currentTimeMillis(); if (remaining > 0) { //int ms = (int) (remaining % 1000); remaining /= 1000; //int sc = (int) (remaining % 60); remaining /= 60; int mn = (int) (remaining % 60); remaining /= 60; int hr = (int) (remaining % 24); long dy = (int) remaining / 24; rtn = dy + "" + hr + "?" + mn + "";//+ sc + ""; } else { rtn = ""; } } catch (ParseException e) { } return rtn; } /** * ? ? 2013-05-31 * @return */ public static String getCurrentDay() { return getBeforeOrAfterDay(0); } /** * ???? ? 2013-05-31 * @return */ public static String getAfterToday() { return getBeforeOrAfterDay(1); } /** * ?date ?? ? 2013-05-31 * @param date * @return */ public static String getAfterToday(Date date) { return getBeforeOrAfterDay(date, 1); } /** * ????? ? 2013-05-31 * @return */ public static String getOneWeekBefore() { return getBeforeOrAfterDay(-7); } /** * ? ? ? daycount * @param daycount * @return */ public static String getBeforeOrAfterDay(int daycount) { Date dayBefore = new Date(); if (daycount != 0) { Calendar calendar = Calendar.getInstance(); // calendar.setTime(new Date());//? calendar.add(Calendar.DAY_OF_MONTH, daycount); // ? + dayCount dayBefore = calendar.getTime(); //? + dayCount } return Date2String(dayBefore); } public static String getBeforeOrAfterDay(Date date, int daycount) { if (daycount != 0) { Calendar calendar = Calendar.getInstance(); // calendar.setTime(date);//? calendar.add(Calendar.DAY_OF_MONTH, daycount); // ? + dayCount date = calendar.getTime(); //? + dayCount } return Date2String(date); } public static Date getBeforeOrAfterDayToDate(Date date, int daycount) { Calendar calendar = null; if (daycount != 0) { calendar = Calendar.getInstance(); // calendar.setTime(date);//? calendar.add(Calendar.DAY_OF_MONTH, daycount); // ? + dayCount } return calendar.getTime(); } /** * ? * @return int */ public static int remainSecondsInToday() { int remainSeconds = DAY_SECONDS; Date date = new Date(); int currentTimeSendcods = date.getHours() * 3600 + date.getMinutes() * 60 + date.getSeconds(); remainSeconds = DAY_SECONDS - currentTimeSendcods; return remainSeconds; } public static void main(String[] args) throws Exception { System.out.println(DateUtils.Date2String(new Date(), "yyyy-MM-dd HH:mm:ss")); //1986-07-05 520873200000 //System.out.println(getDateTime2()); //System.out.println(remainSecondsInToday()); } }