DateUtil
来源:互联网 发布:java进程同步编程 编辑:程序博客网 时间:2024/05/21 09:03
import java.sql.Timestamp;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;public class DateUtil { private static final String dateFormat = "yyyy-MM-dd"; public static final long oneDay = 86400000L; public DateUtil() { } public static Date formatToDate(String dateStr, String format) { Date date = null; try { if(!StringUtil.isEmpty(dateStr)) { date = (new SimpleDateFormat(format)).parse(dateStr); } } catch (ParseException var4) { var4.printStackTrace(); } return date; } public static String formatTimeStampToString(Timestamp date, String format) { if(date == null) { return ""; } else { SimpleDateFormat df = new SimpleDateFormat(format); String str = df.format(date); return str; } } public static Timestamp getCurrentTimeStampYMD() { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd 00:00:00"); String time = df.format(new Date()); Timestamp ts = Timestamp.valueOf(time); return ts; } public static Timestamp formatToTimestamp(String date, String format) { Timestamp timestamp = null; try { if(date != null && date.length() > 0) { Date e = (new SimpleDateFormat(format)).parse(date); timestamp = new Timestamp(e.getTime()); } } catch (ParseException var4) { var4.printStackTrace(); } return timestamp; } public static String formatToDayStr(Date date) { return formatToStr(date, "yyyy-MM-dd"); } public static String formatToStr(Date date, String format) { String dateStr = null; if(date != null) { dateStr = (new SimpleDateFormat(format)).format(date); } return dateStr; } public static String formatToStr(Timestamp date, String format) { String dateStr = null; if(date != null) { dateStr = (new SimpleDateFormat(format)).format(date); } return dateStr; } public static int dateDiff(Date begin, Date end) { return (int)((getDayBegin(end).getTime() - getDayBegin(begin).getTime()) / 86400000L); } public static Date getDayBegin(Date day) { Calendar c = Calendar.getInstance(); c.setTimeInMillis(day.getTime()); c.set(14, 0); c.set(13, 0); c.set(12, 0); c.set(11, 0); return c.getTime(); } public static long dateDiff(String startTime, String endTime, String format) throws ParseException { SimpleDateFormat sd = new SimpleDateFormat(format); long nd = 86400000L; long nh = 3600000L; long nm = 60000L; long ns = 1000L; long diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime(); long day = diff / nd; long hour = diff % nd / nh; long min = diff % nd % nh / nm; long sec = diff % nd % nh % nm / ns; System.out.println("时间相差:" + day + "天" + hour + "小时" + min + "分钟" + sec + "秒。"); return day; } public static String getStrFromDate(Date date) { return getStrFormDate(date, "yyyy-MM-dd"); } public static String getStrFormDate(Date date, String format) { SimpleDateFormat sdf = new SimpleDateFormat(format); String str = sdf.format(date); return str; } public static Date getSpecifiedDay(Date date, int t) { if(date == null) { return null; } else { Calendar c = Calendar.getInstance(); c.setTime(date); c.add(5, t); return c.getTime(); } } public static Date getSpecifiedDayStart(Date date, int t) { if(date == null) { return null; } else { Calendar c = Calendar.getInstance(); c.setTime(date); c.add(5, t); c.set(11, 0); c.set(12, 0); c.set(13, 0); c.set(14, 0); return c.getTime(); } } public static Date getSpecifiedDayEnd(Date date, int t) { if(date == null) { return null; } else { Calendar c = Calendar.getInstance(); c.setTime(date); c.add(5, t); c.set(11, 23); c.set(12, 29); c.set(13, 29); c.set(14, 999); return c.getTime(); } } public static void main(String[] args) throws ParseException { System.out.println(getSpecifiedDayStart(new Date(), 1)); }}
0 0
- dateUtil
- DateUtil
- DateUtil
- DateUtil
- DateUtil
- DateUtil
- DateUtil
- DateUtil
- Dateutil
- DateUtil
- DateUtil
- DateUtil
- DateUtil
- DateUtil
- DateUtil
- DateUtil
- DateUtil
- DateUtil
- 多重继承和 templates
- 2012年的macbook pro 用ssd 替换原有硬盘
- java基本数据类型与引用数据类型的区别
- Uva220黑白棋
- 关于Shiro中的Realm
- DateUtil
- runC源码分析——Create/Run Container
- python爬虫下载网站磁力链接
- 关于自己
- ijkplayer支持h264
- 工作五年的经历、吐槽
- Codeforces 283E
- oracle 忘记密码时,本机登陆时不需要密
- Celery源码分析(一)-------------从命令执行到生成Worker