实际编码中,对时间与日期的操作是非常常见的需求。
写了个比较简单的类,来满足部分对时间相关的操作。后续可以根据需求再添加相关功能。
包含的方法有:
1.返回当前系统时间
2.返回当前时间戳
3.返回上午下午
4.将Date对象变为字符串
5.将字符串变为Date对象
6.根据字符串返回时间戳
7.根据时间戳返回日期
8.得到前一天的时间戳
9.得到前一天的字符串
10.根据岂止日期输出时间序列
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Set;
import java.util.TreeSet;
/**
* Created by WangLei on 16-12-13.
*/
public class TimeParse {
public static final String DATE_YMD_TEMPLATE = "yyyyMMdd";
public static final long TIME_DAY_MILLISECOND = 86400000;
/**
* 返回当前系统时间,返回Date对象
* @return
*/
public static Date getCurrDate() {
return new Date();
}
/**
* 返回当前系统时间戳
* @return
*/
public static long getCurrTimeStamp() {
return new Date().getTime();
}
/**
* 返回上午还是下午
* @return
*/
public static Integer getCurrDateAMorPM() {
Calendar calendar = Calendar.getInstance();
return calendar.get(Calendar.AM_PM);
}
/**
* 根据Date对象返回时间字符串
* @param currDate
* @param format
* @return
*/
public static String getFormatDateStr(Date currDate,String format) {
SimpleDateFormat sdf = null;
try {
sdf = new SimpleDateFormat(format);
return sdf.format(currDate);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 根据日期字符串返回Date对象
* @param currDate
* @param format
* @return
*/
public static Date getFormatDate(String currDate, String format) {
SimpleDateFormat sdf = null;
try {
sdf = new SimpleDateFormat(format);
return sdf.parse(currDate);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 根据日期字符串返回long时间戳
* @param dateStr
* @return
*/
public static long genTimeStampFromYmd(String dateStr) {
return getFormatDate(dateStr,"yyyyMMdd").getTime();
}
/**
* 根据时间戳返回日期字符串
* @param timeStamp
* @param format
* @return
*/
public static String getFormatDateTime(long timeStamp,String format) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(format);
cal.setTimeInMillis(timeStamp);
return sdf.format(cal.getTime());
}
public static String getFormatDateTime(long timeStamp) {
return getFormatDateTime(timeStamp,"yyyyMMdd");
}
/**
* 得到前一天的时间戳
* @param dateStr
* @return
*/
public static long reduceOneDayTimeStamp(String dateStr) {
long timestamp = genTimeStampFromYmd(dateStr);
return reduceOneDayTimeStamp(timestamp);
}
/**
* 得到前一天的时间戳
* @param ts
* @return
*/
public static long reduceOneDayTimeStamp(long ts) {
String dateStr = getFormatDateStr(new Date(ts - TIME_DAY_MILLISECOND),DATE_YMD_TEMPLATE);
return getFormatDate(dateStr,DATE_YMD_TEMPLATE).getTime();
}
/**
* 得到前一天的日期字符串
* @param dateStr
* @return
*/
public static String reduceOneDayTs(String dateStr) {
long timeStamp = genTimeStampFromYmd(dateStr);
return getFormatDateTime(reduceOneDayTimeStamp(timeStamp));
}
/**
*根据起止日期输出时间序列
* @param startTs
* @param stopTs
* @return
*/
public static Set<Long> genTsSet(Long startTs, Long stopTs) throws ParseException{
if(startTs > stopTs) {
throw new ParseException("The startsTs bigger than stopTs!",0);
}
TreeSet tsSet = new TreeSet();
for(long ts = startTs.longValue(); ts <= stopTs; ts += TIME_DAY_MILLISECOND) {
tsSet.add(Long.valueOf(ts));
}
return tsSet;
}
public static void main(String[] args) throws Exception{
String startTs = "20161029";
String stopTs = "20161103";
String res_date = reduceOneDayTs(startTs);
System.out.println("res_date is: " + res_date);
for(long each:genTsSet(genTimeStampFromYmd(startTs),genTimeStampFromYmd(stopTs))) {
String resultDate = getFormatDateTime(each);
System.out.println(resultDate);
}
}
}