Created
August 6, 2014 08:49
-
-
Save yeluolanyan/e162614cebd3fc3ee540 to your computer and use it in GitHub Desktop.
对于时间日期各种格式的处理
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cmdi.util; | |
import java.io.File; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.ArrayList; | |
import java.util.Calendar; | |
import java.util.Date; | |
import java.util.GregorianCalendar; | |
import java.util.List; | |
import com.cmdi.model.User; | |
public class DateUtil { | |
public static final long ONE_HOUR = 60*60*1000L; | |
public static String datetoString(Date date){ | |
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy年MM月dd日 HH时mm分ss秒" ); | |
String str = sdf.format(date); | |
return str; | |
} | |
/** | |
* 得到当前时间 | |
* @return 1358822733362 | |
*/ | |
public static long getCurrentlong(){ | |
return (new Date()).getTime(); | |
} | |
public static String getYesterday() { | |
Date date = new Date(); | |
Calendar calendar = new GregorianCalendar(); | |
calendar.setTime(date); | |
calendar.add(calendar.DATE, -1); | |
date = calendar.getTime(); | |
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); | |
String dateString = formatter.format(date); | |
return dateString; | |
} | |
/** | |
* 得到当前时间 | |
* @return 1358822733362 | |
*/ | |
public static String getCurrentlongStr(){ | |
return String.valueOf(getCurrentlong()); | |
} | |
public static String dateToString(Date date) { | |
java.text.DateFormat df = new java.text.SimpleDateFormat( | |
"yyyy-MM-dd HH:mm:ss"); | |
String s = df.format(date); | |
return s; | |
} | |
public static String longToDateString(long l) { | |
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | |
java.util.Date dt = new Date(l); | |
String sDateTime = null; | |
try { | |
sDateTime = sdf.format(dt); //得到精确到秒的表示:08/31/2006 21:08:00 | |
}catch(Exception e) { | |
e.printStackTrace(); | |
} | |
return sDateTime; | |
} | |
public static String longToDateStringHMS(long l) { | |
SimpleDateFormat sdf= new SimpleDateFormat("HH:mm:ss"); | |
java.util.Date dt = new Date(l); | |
String sDateTime = null; | |
try { | |
sDateTime = sdf.format(dt); //得到精确到秒的表示:08/31/2006 21:08:00 | |
}catch(Exception e) { | |
e.printStackTrace(); | |
} | |
return sDateTime; | |
} | |
public static String longToDateStringYMD(long l) { | |
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd"); | |
java.util.Date dt = new Date(l); | |
String sDateTime = sdf.format(dt); //得到精确到秒的表示 | |
return sDateTime; | |
} | |
public static String longStrToDateStr(String strl) { | |
if(null == strl || "".equals(strl)) { | |
return null; | |
} | |
return longToDateString(Long.parseLong(strl)); | |
} | |
public static String longStrToDateStrYMD(String longTime) { | |
if(null == longTime) { | |
return ""; | |
} | |
return longToDateStringYMD(Long.parseLong(longTime)); | |
} | |
public static String DateToLongString(Date d) { | |
if(null == d || "".equals(d)) { | |
return null; | |
} | |
// Date dt = new Date(); | |
long lSysTime1 = d.getTime(); | |
return String.valueOf(lSysTime1); | |
} | |
/** | |
* | |
* @param str yyyy-MM-dd | |
* @return | |
*/ | |
public static String StringToLong(String str) { | |
if(null == str || str.equals("")) { | |
return null; | |
} | |
try { | |
SimpleDateFormat sdf= new SimpleDateFormat("yy-MM-dd"); | |
Date dt2 = sdf.parse(str); | |
long lTime = dt2.getTime(); | |
return String.valueOf(lTime); | |
}catch(Exception e) { | |
return null; | |
} | |
} | |
/** | |
* | |
* @param str yyyy-MM-dd hh:DD:ss | |
* @return | |
*/ | |
public static String StringToLongss(String str) { | |
if(null == str || str.equals("")) { | |
return null; | |
} | |
try { | |
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | |
Date dt2 = sdf.parse(str); | |
long lTime = dt2.getTime(); | |
return String.valueOf(lTime); | |
}catch(Exception e) { | |
return null; | |
} | |
} | |
/** | |
* 将 yyyy-MM-dd 的字符串时间格式,转换为 毫秒 | |
* @param str | |
* @return | |
* @throws ParseException | |
*/ | |
public static String Stringtolong1(String str) throws ParseException{ | |
if(null == str || str.equals("")) { | |
return null; | |
} | |
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd"); | |
return String.valueOf(sdf2.parse(str).getTime()); | |
} | |
/** | |
* 将 毫秒 转换为 yyyy-MM-dd HH-mm 的字符串时间格式, | |
* @param str | |
* @return | |
* @throws ParseException | |
*/ | |
public static String Longtostring(String str) throws ParseException{ | |
if(null == str || str.equals("")) { | |
return null; | |
} | |
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd hh:mm"); | |
return String.valueOf(sdf2.format(new Date(Long.valueOf(str)))); | |
} | |
/** | |
* | |
* @param str yyyy-MM-dd hh:DD:ss | |
* @return | |
*/ | |
public static String StringToLongsss(String str) { | |
if(null == str || str.equals("")) { | |
return null; | |
} | |
try { | |
SimpleDateFormat sdf= new SimpleDateFormat("yy-MM-dd hh:mm"); | |
Date dt2 = sdf.parse(str); | |
long lTime = dt2.getTime(); | |
return String.valueOf(lTime); | |
}catch(Exception e) { | |
return null; | |
} | |
} | |
public static Long dateStringToLong(String str) { | |
if(null == str || str.equals("")) { | |
return null; | |
} | |
try { | |
SimpleDateFormat sdf= new SimpleDateFormat("yy-MM-dd"); | |
Date dt2 = sdf.parse(str); | |
long lTime = dt2.getTime(); | |
return lTime; | |
}catch(Exception e) { | |
return null; | |
} | |
} | |
public static String addOneDayString(String str) { | |
if(null == str || str.equals("")) { | |
return null; | |
} | |
try { | |
SimpleDateFormat sdf= new SimpleDateFormat("yy-MM-dd"); | |
Date dt2 = sdf.parse(str); | |
long lTime = dt2.getTime() + ONE_HOUR * 24; | |
return String.valueOf(lTime); | |
}catch(Exception e) { | |
return null; | |
} | |
} | |
public static Date getFirstDateOfThisMonth(){ | |
Calendar cal = Calendar.getInstance(); | |
cal.set(cal.DATE,1); | |
return DelHMS(cal.getTime()); | |
} | |
public static Date getFirstDateOfNextMonth() { | |
Calendar cal = Calendar.getInstance(); | |
cal.add(cal.MONTH, 1); | |
cal.set(cal.DATE,1); | |
return DelHMS(cal.getTime()); | |
} | |
public static Date getLastDateOfThisMonth() { | |
Calendar cal = Calendar.getInstance(); | |
cal.add(cal.MONTH, 1); | |
cal.set(cal.DATE,1); | |
cal.add(cal.DATE, -1); | |
return DelHMS(cal.getTime()); | |
} | |
public static Date getFirstDateOfThisYear() { | |
Calendar cal = Calendar.getInstance(); | |
cal.set(cal.DAY_OF_YEAR,1); | |
return DelHMS(cal.getTime()); | |
} | |
public static Date getFirstDateOfNextYear() { | |
Calendar cal = Calendar.getInstance(); | |
cal.add(cal.YEAR, 1); | |
cal.set(cal.DAY_OF_YEAR ,1); | |
return DelHMS(cal.getTime()); | |
} | |
public static Date getLastDateOfThisYear() { | |
Calendar cal = Calendar.getInstance(); | |
cal.add(cal.YEAR, 1); | |
cal.set(cal.DAY_OF_YEAR ,1); | |
cal.add(cal.DATE, -1); | |
return DelHMS(cal.getTime()); | |
} | |
public static Date DelHMS(Date d) { | |
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd"); | |
String dateStr = sdf.format(d); | |
Date d2 = null; | |
try { | |
d2 = sdf.parse(dateStr); | |
} catch (ParseException e) { | |
} | |
return d2; | |
} | |
/** | |
* 某一天到1970年的天数差 | |
* @param dateLong | |
* @return | |
*/ | |
public static String getDaysFrom1970(Long dateLong) { | |
Calendar c = Calendar.getInstance(); | |
c.set(1970, 0, 1,0,0,0); | |
long days = (dateLong - c.getTimeInMillis())/(ONE_HOUR*24); | |
return String.valueOf(days); | |
} | |
public static String getDaysFrom1970(String dateLong) { | |
try { | |
Long date = Long.parseLong(dateLong); | |
return getDaysFrom1970(date); | |
}catch (Exception e) { | |
e.printStackTrace(); | |
return "0"; | |
} | |
} | |
public static String getDateFromDaysFromYMD(String date) { | |
String result = "0"; | |
try { | |
Calendar c = Calendar.getInstance(); | |
c.set(1970, 0, 1,0,0,0); | |
result = longStrToDateStrYMD(String.valueOf(((Long.parseLong(date)) * ONE_HOUR *24 +c.getTimeInMillis()))); | |
}catch(Exception e) { | |
} | |
return result; | |
} | |
/** | |
* 获取当前日期的前一天时间 格式YY-MM-DD | |
* @param args | |
*/ | |
public String getLastDate(){ | |
Calendar c = Calendar.getInstance(); | |
c.add(Calendar.DAY_OF_MONTH, -1); | |
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); | |
String mDateTime=formatter.format(c.getTime()); | |
return mDateTime; | |
} | |
/** | |
* 获取当前日期的前一天时间 格式YY-MM-DD | |
* @param args | |
*/ | |
public static String getTodayDate(){ | |
Calendar c = Calendar.getInstance(); | |
c.add(Calendar.DAY_OF_MONTH,0); | |
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); | |
String mDateTime=formatter.format(c.getTime()); | |
return mDateTime; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment