栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java日期时间操作工具类

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java日期时间操作工具类

本文实例为大家分享了java日期时间操作工具类,供大家参考,具体内容如下

虽然jdk1.8开始,加入了time包,里面对时区,本地化时间,格式化,以及时间等做了很好的封装,但仍然要写一个工具类。大家看着用。应该没有bug。如果发现了,请您一定告知,互相学习!好了,上代码:

package com.wdy.tools.utils.timeutil;
 
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
 
 
public class TimeUtil {
 
 
 public static int getHH() throws Exception {
 DateFormat df = new SimpleDateFormat("HH");
 Date date = new Date(System.currentTimeMillis());
 String str = df.format(date);
 return Integer.parseInt(str);
 }
 
 
 public static int getMM() throws Exception {
 DateFormat df = new SimpleDateFormat("mm");
 Date date = new Date(System.currentTimeMillis());
 String str = df.format(date);
 // return Integer.parseInt(str.split(":")[0]) * 4 +
 // Integer.parseInt(str.split(":")[1]) / 15;
 return Integer.parseInt(str);
 }
 
 
 public static int getSS() throws Exception {
 DateFormat df = new SimpleDateFormat("ss");
 Date date = new Date(System.currentTimeMillis());
 String str = df.format(date);
 // return Integer.parseInt(str.split(":")[0]) * 4 +
 // Integer.parseInt(str.split(":")[1]) / 15;
 return Integer.parseInt(str);
 }
 
 
 public static String getOtherDay(String date, int dayMark) throws Exception {
 DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
 Date dt = df.parse(date);
 Calendar c = Calendar.getInstance();
 c.setTime(dt);
 c.add(Calendar.DAY_OF_MONTH, dayMark);
 String mDateTime = df.format(c.getTime());
 String strStart = mDateTime.substring(0, 10);
 return strStart;
 }
 
 
 public static String getWeekFirstDate(String date) throws Exception {
 DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
 Date dt = df.parse(date);
 Calendar c = Calendar.getInstance();
 c.setTime(dt);
 int days = c.get(Calendar.DAY_OF_WEEK);
 String strStart = "";
 if (days == 1) {
  strStart = getOtherDay(date, -days - 5);
 } else {
  strStart = getOtherDay(date, -days + 2);
 }
 return strStart;
 }
 
 
 public static String getWeekLastDate(String date) throws Exception {
 DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
 Date dt = df.parse(date);
 Calendar c = Calendar.getInstance();
 c.setTime(dt);
 int days = c.get(Calendar.DAY_OF_WEEK);
 String strStart = "";
 if (days == 1) {
  strStart = getOtherDay(date, 0);
 } else {
  strStart = getOtherDay(date, 8 - days);
 }
 return strStart;
 }
 
 
 public static String getOtherWeekFirstDate(String date, int weekMark)
  throws Exception {
 String firstDate = getWeekFirstDate(date);
 DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
 Date dt = df.parse(firstDate);
 Calendar c = Calendar.getInstance();
 c.setTime(dt);
 c.add(Calendar.WEEK_OF_YEAR, weekMark);
 String mDateTime = df.format(c.getTime());
 String strStart = mDateTime.substring(0, 10);
 return strStart;
 }
 
 
 public static String getSeasonFirstDate(String date) throws Exception {
 DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
 Date dt = df.parse(date);
 Calendar c = Calendar.getInstance();
 c.setTime(dt);
 int year = c.get(Calendar.YEAR);
 int month = c.get(Calendar.MONTH);
 if (month < 3) {
  month = 0;
 } else if (month >= 3 && month < 6) {
  month = 3;
 } else if (month >= 6 && month < 9) {
  month = 6;
 } else if (month >= 9 && month < 12) {
  month = 9;
 }
 c.set(year, month, 1);
 
 String mDateTime = df.format(c.getTime());
 String strStart = mDateTime.substring(0, 10);
 return strStart;
 }
 
 
 public static String getOtherSeasonFirstDate(String date, int seasonMark)
  throws Exception {
 String firstDate = getSeasonFirstDate(date);
 DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
 Date dt = df.parse(firstDate);
 Calendar c = Calendar.getInstance();
 c.setTime(dt);
 int month = seasonMark * 3;
 if (month != 0) {
  c.add(Calendar.MONTH, month);
 }
 String mDateTime = df.format(c.getTime());
 String strStart = mDateTime.substring(0, 10);
 return strStart;
 }
 
 
 public static String getMonthFirstDate(String date) throws Exception {
 DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
 Date dt = df.parse(date);
 Calendar c = Calendar.getInstance();
 c.setTime(dt);
 int year = c.get(Calendar.YEAR);
 int month = c.get(Calendar.MONTH);
 c.set(year, month, 1);
 String mDateTime = df.format(c.getTime());
 String strStart = mDateTime.substring(0, 10);
 return strStart;
 }
 
 
 public static String getMonthLastDate(String date) throws Exception {
 DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
 Date dt = df.parse(date);
 Calendar c = Calendar.getInstance();
 c.setTime(dt);
 int year = c.get(Calendar.YEAR);
 int month = c.get(Calendar.MONTH);
 int dayNum = c.getActualMaximum(Calendar.DAY_OF_MONTH);
 c.set(year, month, dayNum);
 String mDateTime = df.format(c.getTime());
 String strStart = mDateTime.substring(0, 10);
 return strStart;
 }
 
 
 public static String getOtherMonthFirstDate(String date, int monthMark)
  throws Exception {
 String firstDate = getMonthFirstDate(date);
 DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
 Date dt = df.parse(firstDate);
 Calendar c = Calendar.getInstance();
 c.setTime(dt);
 c.add(Calendar.MONTH, monthMark);
 String mDateTime = df.format(c.getTime());
 String strStart = mDateTime.substring(0, 10);
 return strStart;
 }
 
 
 public static String getYearFirstDate(String date) throws Exception {
 DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
 Date dt = df.parse(date);
 Calendar c = Calendar.getInstance();
 c.setTime(dt);
 int year = c.get(Calendar.YEAR);
 c.set(year, 0, 1);
 String mDateTime = df.format(c.getTime());
 String strStart = mDateTime.substring(0, 10);
 return strStart;
 }
 
 
 public static String getOtherYearFirstDate(String date, int yearMark)
  throws Exception {
 String firstDate = getMonthFirstDate(date);
 DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
 Date dt = df.parse(firstDate);
 Calendar c = Calendar.getInstance();
 c.setTime(dt);
 c.add(Calendar.YEAR, yearMark);
 String mDateTime = df.format(c.getTime());
 String strStart = mDateTime.substring(0, 4);
 return strStart + "-01-01";
 }
 
 
 public static String getYearTqDay(String date, int year) throws Exception {
 DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
 Date dt = df.parse(date);
 Calendar c = Calendar.getInstance();
 c.setTime(dt);
 c.add(Calendar.YEAR, year);
 String mDateTime = df.format(c.getTime());
 return mDateTime;
 }
 
 
 public static String getMonthTqDay(String date, int month) throws Exception {
 DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
 Date dt = df.parse(date);
 Calendar c = Calendar.getInstance();
 c.setTime(dt);
 c.add(Calendar.MONTH, month);
 String mDateTime = df.format(c.getTime());
 return mDateTime;
 }
 
 
 public static String getTbMonth(String month) throws Exception {
 DateFormat df = new SimpleDateFormat("yyyy-MM");
 Date dt = df.parse(month);
 Calendar c = Calendar.getInstance();
 c.setTime(dt);
 c.add(Calendar.YEAR, -1);
 String mDateTime = df.format(c.getTime());
 return mDateTime;
 }
 
 
 public static String getHbMonth(String month) throws Exception {
 DateFormat df = new SimpleDateFormat("yyyy-MM");
 Date dt = df.parse(month);
 Calendar c = Calendar.getInstance();
 c.setTime(dt);
 c.add(Calendar.MONTH, -1);
 String mDateTime = df.format(c.getTime());
 return mDateTime;
 }
 
 
 public static int getDaysOfTwoDate(String sDate, String eDate)
  throws Exception {
 
 DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
 DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");
 Date date1 = df1.parse(sDate);
 Date date2 = df2.parse(eDate);
 if (null == date1 || null == date2) {
  return -1;
 }
 long intervalMilli = date2.getTime() - date1.getTime();
 return (int) (intervalMilli / (24 * 60 * 60 * 1000));
 }
 
 
 public static int getMonthOfTwoMonth(String sDate, String eDate)
  throws Exception {
 
 DateFormat df = new SimpleDateFormat("yyyy-MM");
 
 Date date1 = df.parse(sDate);
 Date date2 = df.parse(eDate);
 if (null == date1 || null == date2) {
  return -1;
 }
 
 Calendar c1 = Calendar.getInstance();
 c1.setTime(date1);
 Calendar c2 = Calendar.getInstance();
 c2.setTime(date2);
 
 int month1 = c1.get(Calendar.YEAR) * 12 + c1.get(Calendar.MONTH);
 int month2 = c2.get(Calendar.YEAR) * 12 + c2.get(Calendar.MONTH);
 
 return month2 - month1;
 }
 
 
 public static boolean compareDate(String sDate, String eDate, String pattern)
  throws Exception {
 
 DateFormat df1 = new SimpleDateFormat(pattern);
 Date date1 = df1.parse(sDate);
 Date date2 = df1.parse(eDate);
 if (null == date1 || null == date2) {
  return false;
 }
 long intervalMilli = date2.getTime() - date1.getTime();
 if (intervalMilli > 0) {
  return true;
 }
 return false;
 }
 
 
 public static int getMinsOfTwoDate(String sDate, String eDate)
  throws Exception {
 
 DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 Date date1 = df1.parse(sDate);
 Date date2 = df2.parse(eDate);
 if (null == date1 || null == date2) {
  return -1;
 }
 long intervalMilli = date2.getTime() - date1.getTime();
 return (int) (intervalMilli / (60 * 1000));
 }
 
 
 public static String getToDayAllStr() throws Exception {
 DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 Date date = new Date(System.currentTimeMillis());
 String str = df.format(date);
 return str;
 }
 
 
 public static String getToDayDateStr() throws Exception {
 DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
 Date date = new Date(System.currentTimeMillis());
 String str = df.format(date);
 return str;
 }
 
 
 public static String getToDayYmd() throws Exception {
 DateFormat df = new SimpleDateFormat("yyyyMMdd");
 Date date = new Date(System.currentTimeMillis());
 String str = df.format(date);
 return str;
 }
 
 
 public static String getToDayStrByPattern(String pattern) throws Exception {
 DateFormat df = new SimpleDateFormat(pattern);
 Date date = new Date(System.currentTimeMillis());
 String str = df.format(date);
 return str;
 }
 
 
 public static String getToDayHmsStr() throws Exception {
 DateFormat df = new SimpleDateFormat("HH:mm:ss");
 Date date = new Date(System.currentTimeMillis());
 String str = df.format(date);
 return str;
 }
 
 
 public static String getToDayHms() throws Exception {
 DateFormat df = new SimpleDateFormat("HHmmss");
 Date date = new Date(System.currentTimeMillis());
 String str = df.format(date);
 return str;
 }
 
 
 public static String getToDayHmsByPattern(String pattern) throws Exception {
 DateFormat df = new SimpleDateFormat(pattern);
 Date date = new Date(System.currentTimeMillis());
 String str = df.format(date);
 return str;
 }
 
 
 public static String getHmsStrForDateTime(String dayStr) throws Exception {
 DateFormat df = new SimpleDateFormat("HHmmss");
 DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 String str = df.format(df2.parse(dayStr));
 return str;
 }
 
 
 public static String changeDateType(String str, String oldPattern, String newPattern) throws Exception {
 DateFormat df = new SimpleDateFormat(oldPattern);
 DateFormat df1 = new SimpleDateFormat(newPattern);
 return df1.format(df.parse(str));
 }
 
 
 
 public static String getOtherHour(String date, int dayMark)
  throws Exception {
 DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 Date dt = df.parse(date);
 Calendar c = Calendar.getInstance();
 c.setTime(dt);
 c.add(Calendar.HOUR_OF_DAY, dayMark);
 String mDateTime = df.format(c.getTime());
 String strStart = mDateTime;
 return strStart;
 }
 
 
 public static String getOtherMinute(String date, int minuteMark)
  throws Exception {
 DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 Date dt = df.parse(date);
 Calendar c = Calendar.getInstance();
 c.setTime(dt);
 c.add(Calendar.MINUTE, minuteMark);
 String mDateTime = df.format(c.getTime());
 String strStart = mDateTime;
 return strStart;
 }
 
 
 
 public static Date parseDate(String date, String pattern) throws Exception {
 Date returnDate = null;
 if (pattern == null || pattern.equals("") || pattern.equals("null")) {
  pattern = "yyyy-MM-dd HH:mm:ss";
 }
 java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(pattern);
 try {
  returnDate = sdf.parse(date);
 } catch (Exception e) {
  e.printStackTrace();
 }
 return returnDate;
 }
 
 
 public static String formatDate(Date date, String pattern) throws Exception {
 String returnDate = null;
 
 if (date == null) {
  return "";
 }
 
 if (pattern == null || pattern.equals("") || pattern.equals("null")) {
  pattern = "yyyy-MM-dd HH:mm:ss";
 }
 
 java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(pattern);
 returnDate = sdf.format(date);
 
 return returnDate;
 }
 
 
 public static String getSystemMonth() {
 java.util.Date date = new java.util.Date();
 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
 String mDateTime1 = formatter.format(date);
 return mDateTime1;
 }
 
 
 public static String getYearLastMonth(String month, int m) throws Exception {
 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
 
 Date newDate = new Date();
 newDate = format.parse(month);
 Calendar c = Calendar.getInstance();
 c.setTime(newDate);
 c.add(Calendar.YEAR, m);
 
 newDate.setTime(c.getTimeInMillis());
 
 return format.format(newDate).substring(0, 4) + "-12";
 }
 
 
 public static String getOtherMonth(String month, int m) throws Exception {
 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
 
 Date newDate = new Date();
 newDate = format.parse(month);
 Calendar c = Calendar.getInstance();
 c.setTime(newDate);
 c.add(Calendar.MONTH, m);
 
 newDate.setTime(c.getTimeInMillis());
 
 return format.format(newDate);
 }
 
 
 public static String getOtherMonthYMD(String month, int m) throws Exception {
 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
 
 Date newDate = new Date();
 newDate = format.parse(month);
 Calendar c = Calendar.getInstance();
 c.setTime(newDate);
 c.add(Calendar.MONTH, m);
 
 newDate.setTime(c.getTimeInMillis());
 
 return format.format(newDate);
 }
 
 
 public static int getDaysOfMonth(String monthStr) throws Exception {
 
 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
 Date date = format.parse(monthStr);
 Calendar calendar = new GregorianCalendar();
 calendar.setTime(date);
 int dayNum = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
 return dayNum;
 }
 
 
 public static String getBeforeAfterTimeForMin(String time, int m)
  throws Exception {
 Calendar now = Calendar.getInstance();
 SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
 Date newDate = new Date();
 newDate = format.parse(time);
 now.setTime(newDate);
 now.add(Calendar.MINUTE, m);
 return format.format(now.getTime());
 }
 
 
 public static String getBeforeAfterDateTimeByTime(String time, int m,
  String pattern) throws Exception {
 Calendar now = Calendar.getInstance();
 SimpleDateFormat sdf = new SimpleDateFormat(pattern);
 
 Date newDate = new Date();
 newDate = sdf.parse(time);
 now.setTime(newDate);
 now.add(Calendar.MINUTE, m);
 
 return sdf.format(now.getTime());
 }
 
}

这就是整个工具类了,供君参考。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/139477.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号