JAVA 公共方法 之 date时间处理
来源:互联网 发布:航天金税软件安装 编辑:程序博客网 时间:2024/06/10 03:57
<span style="font-size:18px;"><span style="font-size:18px;">/*** 字符串转换成Date类型* @param strDate* @return*/public static Date getDateFromString(String strDate) {<span style="white-space:pre"></span>if (StringUtils.isBlank(strDate)) {<span style="white-space:pre"></span>return new Date();<span style="white-space:pre"></span>}SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");Date date = null;try {date = simple.parse(strDate);} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}return date;}</span></span>
新建一个DateUtil.java类文件
<span style="font-size:18px;">package com.ffkj.education.util;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;public class DateUtils { public static Date getActionEndTime(Date date, int minute) { Calendar c = Calendar.getInstance(); c.setTime(date); c.add(Calendar.MINUTE, minute); date = c.getTime(); return date; } public static Date getReportEndDay(Date date) { SimpleDateFormat f = new SimpleDateFormat("yyyy-MM"); String start = f.format(date) + "-01"; return parseDate(start); } public static Date parseDate(String start) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); try { Date date = df.parse(start); return date; } catch (ParseException e) { throw new RuntimeException(e); } } public static Calendar getCalendar(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendar; } public static Date addtDay(Date date, int number) { Calendar c = getCalendar(date); c.add(Calendar.DAY_OF_MONTH, number); return c.getTime(); } public static Date getLastDay() { Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.MONTH, 1); calendar.set(Calendar.DAY_OF_MONTH, 0); return calendar.getTime(); } public static Date getLastDay(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.MONTH, 1); calendar.set(Calendar.DAY_OF_MONTH, 0); return calendar.getTime(); } public static Date nextYear(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); c.add(Calendar.YEAR, 1); date = c.getTime(); return date; } public static Date getNowDateShort() throws ParseException { Date currentTime = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); String dateString = formatter.format(currentTime); return formatter.parse(dateString); } public static Date getDateFromStringByYYMMDD(String strDate){ SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd"); Date date=null; try { date= simple.parse(strDate); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return date; } public static Date getNextDay(Date date) { Calendar c = getCalendar(date); c.add(Calendar.DAY_OF_MONTH, 1); return c.getTime(); } public static Date getPreDay(Date date) { Calendar c = getCalendar(date); c.add(Calendar.DAY_OF_MONTH, -1); return c.getTime(); } /** * 字符串转换成Date类型 格式为:【yyyy-MM-dd】 * @param strDate * @return */ public static Date getDateFromString(String strDate) { if (StringUtils.isBlank(strDate)) { return new Date(); } SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd"); Date date = null; try { date = simple.parse(strDate); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return date; } /** * 字符串转换成Date类型 格式为:【yyyy/MM/dd】 * @param strDate * @return */ public static Date getDateFromStringBySqlDate(String strDate) { if (StringUtils.isBlank(strDate)) { return new Date(); } SimpleDateFormat simple = new SimpleDateFormat("yyyy/MM/dd"); Date date = null; try { date = simple.parse(strDate); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return date; } public static Date getDateFromStringBySqlDateYYYYMMDDHHMMSS(String strDate) { if (StringUtils.isBlank(strDate)) { return new Date(); } SimpleDateFormat simple = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss"); Date date = null; try { date = simple.parse(strDate); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return date; } public static String getStringDateYYYYMMDD(Date date) { if (date == null) { return ""; } SimpleDateFormat simple = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss"); String newDate = simple.format(date); return newDate; } public static String getDateFromStringYYYYMMDD(Date date) { if (date == null) { return ""; } SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd"); String newDate = simple.format(date); return newDate; } public static String getDateFromStringYYYYMMDDHHMM(Date date) { if (date == null) { return ""; } SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm"); String newDate = simple.format(date); return newDate; } public static String getDateFromString(Date date) { if (date == null) { return ""; } SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String newDate = simple.format(date); return newDate; } public static String getDateFromStringYYYMMDD(Date date){ SimpleDateFormat simple = new SimpleDateFormat ("yyyy-MM-dd"); String newDate = simple.format(date); return newDate; } public static Date getDateFromDate(Date date) { SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd"); String newString = simple.format(date); Date newdate = null; try { newdate = simple.parse(newString); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return newdate; } public static String cal(int second) { int h = 0; int d = 0; int s = 0; int temp = second % 3600; if (second > 3600) { h = second / 3600; if (temp != 0) { if (temp > 60) { d = temp / 60; if (temp % 60 != 0) { s = temp % 60; } } else { s = temp; } } } else { d = second / 60; if (second % 60 != 0) { s = second % 60; } } return h + "\u65F6" + d + "\u5206" + s + "\u79D2"; } public static String getDate(Date date) { long miaoshu = date.getTime(); return String.valueOf(miaoshu); } // a integer to xx:xx:xx public static String secToTime(int time) { String timeStr = null; int hour = 0; int minute = 0; int second = 0; if (time <= 0) return "00:00:00"; else { minute = time / 60; if (minute < 60) { second = time % 60; timeStr = "00:" + unitFormat(minute) + ":" + unitFormat(second); } else { hour = minute / 60; if (hour > 99) return "99:59:59"; minute = minute % 60; second = time - hour * 3600 - minute * 60; timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second); } } return timeStr; } public static String unitFormat(int i) { String retStr = null; if (i >= 0 && i < 10) retStr = "0" + Integer.toString(i); else retStr = "" + i; return retStr; }}</span>
1 0
- JAVA 公共方法 之 date时间处理
- java 时间处理 Date
- python时间处理之date
- python时间处理之date
- python时间处理之date
- java Date 时间 日期 处理
- java date时间相关处理
- JAVA 处理时间 - java.sql.Date、java.util.Date与数据库中的Date字段的转换方法
- JAVA 处理时间 - java.sql.Date、java.util.Date与数据库中的Date字段的转换方法
- JAVA 处理时间 - java.sql.Date、java.util.Date与数据库中的Date字段的转换方法
- JAVA 处理时间 - java.sql.Date、java.util.Date与数据库中的Date字段的转换方法
- JAVA 处理时间 - java.sql.Date、java.util.Date与数据库中的Date字段的转换方法
- JAVA 处理时间 - java.sql.Date、java.util.Date与数据库中的Date字段的转换方法
- JAVA 处理时间 - java.sql.Date、java.util.Date与数据库中的Date字段的转换方法[转]
- Java之日期时间常用的公共方法UtilForDate
- java处理图片公共方法
- java-时间处理:Date,DateFomat,Calendar
- [Java]Date时间的相关处理
- java.util.Timer分析源码了解原理
- 4542: [Hnoi2016]大数 莫队算法
- 【bzoj2091】【Poi2010】【The Minima Game】【dp】
- 【BZOJ4300】绝世好题,位运算相关DP
- Android开发中的View简介
- JAVA 公共方法 之 date时间处理
- 防盗链apacher配置规则
- Timer,TimerTask,TaskQueue,TimerThread原理关系图
- js 控制台输出
- 跨国风投公司加入区块链投资领域
- 【BZOJ3622】已经没有什么好害怕的了【计数DP】【姿势】
- 在VMware上安装ubuntu server版本的虚拟机
- Java程序通用场景性能分析
- winsocket(1)