DateUtil
/**
* @author
* @date 2017年10月17日-下午1:56:05
*/
package com.zte.claa.infiboss.utils;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;
public class DateUtil {
// datetimeFormat
public static final SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
// 防止例項化
private DateUtil(){
}
/**
* 格式化日期.
*
* @param Date
* @return yyyy-MM-dd 格式的字串
*/
public static String formatDate(Date d){
return dateFormat.format(d);
}
/**
*
* @param t
* @return yyyy-MM-dd HH:mm:ss 格式字串.
*/
public static String formatTimestamp(Timestamp t){
return datetimeFormat.format(t);
}
/**
*
* @param tm
* @return
*/
public static String formatDateTime(long tm){
return datetimeFormat.format(new Date(tm));
}
/**
* 解析日期時間字串
*
* @param dateStr
* @return
*/
public static Timestamp parseDatetime(String dateStr){
try {
Date d = datetimeFormat.parse(dateStr);
return new Timestamp(d.getTime());
} catch (ParseException e) {
return null;
}
}
/**
* 解析日期時間字串
*
* @param dateStr
* @return
*/
public static Timestamp parseDate(String dateStr){
try {
Date d = dateFormat.parse(dateStr);
return new Timestamp(d.getTime());
} catch (ParseException e) {
return null;
}
}
/**
* 解析日期時間字串
*
* @param timeStr HH:mm:ss
* @return date
*/
public static Timestamp parseDateWithFixTime(Date d, String timeStr){
try {
String dStr = dateFormat.format(d);
Date dt = datetimeFormat.parse(dStr+" "+timeStr);
return new Timestamp(dt.getTime());
} catch (ParseException e) {
return null;
}
}
/**
* 解析日期時間字串
*
* @param timeStr HH:mm:ss
* @return date
*/
public static Timestamp parseCurDateWithFixTime(String timeStr){
return DateUtil.parseDateWithFixTime(new Date(System.currentTimeMillis()), timeStr);
}
/**
* 返回日期是否在指定的日期範圍內
* @param date
* @param startDate
* @param endDate
* @return
*/
public static boolean isDateInRange(Date date, Timestamp startDate, Timestamp endDate){
if (date.before(startDate) || date.after(endDate)){
return false;
}
return true;
}
/**
* 獲取當月的第一天
* @param date
* @return
*/
public static Date getFirstDayThisMonth(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
public static void main(String[] args){
/*
String str = "2017-10-17";
Timestamp ts1 = DateUtil.parseDate(str);
System.out.println(new Date(ts1.getTime()));
str = "2017-10-17 01:00:00";
Timestamp ts2 = DateUtil.parseDatetime(str);
System.out.println(new Date(ts2.getTime()));
System.out.println(DateUtil.parseCurDateWithFixTime("11:10:00"));
*/
long ts = 1532584842 * 1000L;
System.out.println(new Date(ts));
if(true){
return;
}
for (int i = 0; i < 50; i++) {
UUID uuid = UUID.randomUUID();
StringBuffer buf = new StringBuffer("U");
buf.append(Math.abs(uuid.getLeastSignificantBits())).append(" -- ");
buf.append(Long.toUnsignedString(uuid.getLeastSignificantBits()));
// buf.append(Long.toUnsignedString(uuid.getMostSignificantBits(),16).toUpperCase());
System.out.println(buf.toString());
}
}
}