java時間轉換工具類
package com.ikcrm.lib.common.utils; import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.List; public class StartAndEndDateUrils { //獲取當天的開始時間 public static Date getDayBegin() { Calendar cal = new GregorianCalendar(); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); return cal.getTime(); } //獲取當天的結束時間 public static Date getDayEnd() { Calendar cal = new GregorianCalendar();cal.set(Calendar.HOUR_OF_DAY, 23); cal.set(Calendar.MINUTE, 59); cal.set(Calendar.SECOND, 59); return cal.getTime(); } //獲取昨天的開始時間 public static Date getBeginDayOfYesterday() { Calendar cal = new GregorianCalendar(); cal.setTime(getDayBegin()); cal.add(Calendar.DAY_OF_MONTH, -1); returncal.getTime(); } //獲取昨天的結束時間 public static Date getEndDayOfYesterDay() { Calendar cal = new GregorianCalendar(); cal.setTime(getDayEnd()); cal.add(Calendar.DAY_OF_MONTH, -1); return cal.getTime(); } //獲取明天的開始時間 public static Date getBeginDayOfTomorrow() { Calendar cal = new GregorianCalendar(); cal.setTime(getDayBegin()); cal.add(Calendar.DAY_OF_MONTH, 1); return cal.getTime(); } //獲取明天的結束時間 public static Date getEndDayOfTomorrow() { Calendar cal = new GregorianCalendar(); cal.setTime(getDayEnd()); cal.add(Calendar.DAY_OF_MONTH, 1); return cal.getTime(); } //獲取本週的開始時間 public static Date getBeginDayOfWeek() { Date date = new Date(); if (date == null) { return null; } Calendar cal = Calendar.getInstance(); cal.setTime(date); int dayofweek = cal.get(Calendar.DAY_OF_WEEK); if (dayofweek == 1) { dayofweek += 7; } cal.add(Calendar.DATE, 2 - dayofweek); return getDayStartTime(cal.getTime()); } //獲取本週的結束時間 public static Date getEndDayOfWeek(){ Calendar cal = Calendar.getInstance(); cal.setTime(getBeginDayOfWeek()); cal.add(Calendar.DAY_OF_WEEK, 6); Date weekEndSta = cal.getTime(); return getDayEndTime(weekEndSta); } //獲取下週的開始時間 public static Date getNextBeginDayOfWeek() { Date date = new Date(); if (date == null) { return null; } Calendar cal = Calendar.getInstance(); cal.setTime(date); int week = cal.get(Calendar.DAY_OF_WEEK); if(week>2){ cal.add(Calendar.DAY_OF_MONTH,-(week-2)+7); }else{ cal.add(Calendar.DAY_OF_MONTH,2-week+7); } return getDayStartTime(cal.getTime()); } //獲取下週的結束時間 public static Date getNextEndDayOfWeek(){ Date date = new Date(); Calendar cal = Calendar.getInstance(); cal.setTime(date); int week = cal.get(Calendar.DAY_OF_WEEK); if(week>1){ cal.add(Calendar.DAY_OF_MONTH,-(week-1)+7+7); }else{ cal.add(Calendar.DAY_OF_MONTH,1-week+7+7); } Date weekEndSta = cal.getTime(); return getDayEndTime(weekEndSta); } //獲取上週的開始時間 public static Date getLastWeekMonday() { Date date = new Date(); if (date == null) { return null; } Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.WEEK_OF_YEAR, -1);// 一週 cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); return getDayStartTime(cal.getTime()); } //獲取上週的結束時間 public static Date getLastWeekSunday() { Date date = new Date(); if (date == null) { return null; } Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.DAY_OF_WEEK, 1); return getDayStartTime(cal.getTime()); } //獲取本月的開始時間 public static Date getBeginDayOfMonth() { Calendar calendar = Calendar.getInstance(); calendar.set(getNowYear(), getNowMonth() - 1, 1); return getDayStartTime(calendar.getTime()); } //獲取本月的結束時間 public static Date getEndDayOfMonth() { Calendar calendar = Calendar.getInstance(); calendar.set(getNowYear(), getNowMonth() - 1, 1); int day = calendar.getActualMaximum(5); calendar.set(getNowYear(), getNowMonth() - 1, day); return getDayEndTime(calendar.getTime()); } //獲取上月的開始時間 public static Date getBeforeFirstMonthdate() { Calendar calendar=Calendar.getInstance(); calendar.add(Calendar.MONTH, -1); calendar.set(Calendar.DAY_OF_MONTH, 1); return getDayStartTime(calendar.getTime()); } //獲取上月的結束時間 public static Date getBeforeLastMonthdate(){ Calendar calendar=Calendar.getInstance(); int month=calendar.get(Calendar.MONTH); calendar.set(Calendar.MONTH, month-1); calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); return getDayStartTime(calendar.getTime()); } /** * * 描述:獲取下一個月的第一天. * * @return */ public static Date getPerFirstDayOfMonth() { Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.MONTH, 1); calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH)); return getDayStartTime(calendar.getTime()); } /** * * 描述:獲取下一個月的最後一天. * * @return */ public static Date getFinallyDayOfMonth() { Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.MONTH, 1); calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); return getDayEndTime(calendar.getTime()); } //獲取本年的開始時間 public static java.util.Date getBeginDayOfYear() { Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, getNowYear()); // cal.set cal.set(Calendar.MONTH, Calendar.JANUARY); cal.set(Calendar.DATE, 1); return getDayStartTime(cal.getTime()); } //獲取本年的結束時間 public static java.util.Date getEndDayOfYear() { Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, getNowYear()); cal.set(Calendar.MONTH, Calendar.DECEMBER); cal.set(Calendar.DATE, 31); return getDayEndTime(cal.getTime()); } //獲取某個日期的開始時間 public static Timestamp getDayStartTime(Date d) { Calendar calendar = Calendar.getInstance(); if(null != d) calendar.setTime(d); calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0); calendar.set(Calendar.MILLISECOND, 0); return new Timestamp(calendar.getTimeInMillis()); } //獲取某個日期的結束時間 public static Timestamp getDayEndTime(Date d) { Calendar calendar = Calendar.getInstance(); if(null != d) calendar.setTime(d); calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59); calendar.set(Calendar.MILLISECOND, 999); return new Timestamp(calendar.getTimeInMillis()); } //獲取今年是哪一年 public static Integer getNowYear() { Date date = new Date(); GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance(); gc.setTime(date); return Integer.valueOf(gc.get(1)); } //獲取本月是哪一月 public static int getNowMonth() { Date date = new Date(); GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance(); gc.setTime(date); return gc.get(2) + 1; } //兩個日期相減得到的天數 public static int getDiffDays(Date beginDate, Date endDate) { if (beginDate == null || endDate == null) { throw new IllegalArgumentException("getDiffDays param is null!"); } long diff = (endDate.getTime() - beginDate.getTime()) / (1000 * 60 * 60 * 24); int days = new Long(diff).intValue(); return days; } //兩個日期相減得到的毫秒數 public static long dateDiff(Date beginDate, Date endDate) { long date1ms = beginDate.getTime(); long date2ms = endDate.getTime(); return date2ms - date1ms; } //獲取兩個日期中的最大日期 public static Date max(Date beginDate, Date endDate) { if (beginDate == null) { return endDate; } if (endDate == null) { return beginDate; } if (beginDate.after(endDate)) { return beginDate; } return endDate; } //獲取兩個日期中的最小日期 public static Date min(Date beginDate, Date endDate) { if (beginDate == null) { return endDate; } if (endDate == null) { return beginDate; } if (beginDate.after(endDate)) { return endDate; } return beginDate; } //返回某月該季度的第一個月 public static Date getFirstSeasonDate(Date date) { final int[] SEASON = { 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4 }; Calendar cal = Calendar.getInstance(); cal.setTime(date); int sean = SEASON[cal.get(Calendar.MONTH)]; cal.set(Calendar.MONTH, sean * 3 - 3); return cal.getTime(); } //返回某個日期下幾天的日期 public static Date getNextDay(Date date, int i) { Calendar cal = new GregorianCalendar(); cal.setTime(date); cal.set(Calendar.DATE, cal.get(Calendar.DATE) + i); return cal.getTime(); } //返回某個日期前幾天的日期 public static Date getFrontDay(Date date, int i) { Calendar cal = new GregorianCalendar(); cal.setTime(date); cal.set(Calendar.DATE, cal.get(Calendar.DATE) - i); return cal.getTime(); } //獲取某年某月到某年某月按天的切片日期集合(間隔天數的日期集合) public static List getTimeList(int beginYear, int beginMonth, int endYear, int endMonth, int k) { List list = new ArrayList(); if (beginYear == endYear) { for (int j = beginMonth; j <= endMonth; j++) { list.add(getTimeList(beginYear, j, k)); } } else { { for (int j = beginMonth; j < 12; j++) { list.add(getTimeList(beginYear, j, k)); } for (int i = beginYear + 1; i < endYear; i++) { for (int j = 0; j < 12; j++) { list.add(getTimeList(i, j, k)); } } for (int j = 0; j <= endMonth; j++) { list.add(getTimeList(endYear, j, k)); } } } return list; } //獲取某年某月按天切片日期集合(某個月間隔多少天的日期集合) public static List getTimeList(int beginYear, int beginMonth, int k) { List list = new ArrayList(); Calendar begincal = new GregorianCalendar(beginYear, beginMonth, 1); int max = begincal.getActualMaximum(Calendar.DATE); for (int i = 1; i < max; i = i + k) { list.add(begincal.getTime()); begincal.add(Calendar.DATE, k); } begincal = new GregorianCalendar(beginYear, beginMonth, max); list.add(begincal.getTime()); return list; } /** * 根據日期獲得星期 * @param date * @return */ public static String getWeekOfDate(Date date) { String[] weekDaysName = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; String[] weekDaysCode = { "0", "1", "2", "3", "4", "5", "6" }; Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int intWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1; return weekDaysName[intWeek]; } public static void main(String[] args) { // System.out.println(getBeginDayOfWeek()); // System.out.println(getEndDayOfWeek()); // System.out.println(getLastWeekMonday()); // System.out.println(getLastWeekSunday()); // System.out.println(getBeforeFirstMonthdate()); // System.out.println(getBeforeLastMonthdate()); // System.out.println(getWeekOfDate(DateUtils.getCurrentDate())); System.out.println(getPerFirstDayOfMonth()); System.out.println(getFinallyDayOfMonth()); } }
相關推薦
java時間轉換工具類
package com.ikcrm.lib.common.utils; import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.util.ArrayList; import java
日期時間轉換工具類
import org.apache.commons.lang3.StringUtils; import java.time.*; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; imp
java json轉換工具類
在java專案中,通常會用到json型別的轉換,常常需要對 json字串和物件進行相互轉換。 在製作自定義的json轉換類之前,先引入以下依賴 <!--json解析工具--> <dependency> <groupId>com.fasterx
Android 時間轉換工具類
在做聊天頁面的時候,難免遇到要把一個時間戳轉換為一個好看的格式,每次都動手寫也太累了,在這裡給大家分享一下我的工具類吧 其中time_year、time_month等 在文字最後貼出來給大家哦。 /** * 時間轉換工具 */ public class TimeUtil {
java時間日期工具類
package com.bigname.common; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date;
java 時間操作工具類
時間操作工具類CalendarUtil 裡面包含方法有: 獲取當前時間getInstance 字串按照格式轉換成時間strToDate 時間格式化dateToStr 獲取當天零點getSame
時間轉換工具類
package com.wd.tech.app.utils; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; /** * @Projec
Java 型別轉換 工具類
public class TypeCastHelper { private static final String DEFAULT_STRING = ""; private static final int DEFAULT_INT = 0; priv
Java時間格式轉換工具類
string stact 修改 stack time mil 日期格式轉換 pla gettime 把當前時間修改成指定時間 //把當前時間修改成指定時間 public String dateUtil(Integer seconds, String dateFo
Java生成隨機數工具類,進位制之間的轉換工具類,獲取指定時間,時間格式轉換工具類
廢話不多說,貢獻一下code 1.編號生成工具 import org.apache.commons.lang3.StringUtils; import java.math.BigInteger; import java.text.SimpleDa
java時間戳轉換工具類
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; /** *
Java中將字符串與unicode的相互轉換工具類
png style nic ava param 字符串拼接 size tostring info unicode編碼規則 unicode碼對每一個字符用4位16進制數表示。具體規則是:將一個字符(char)的高8位與低8位分別取出,轉化為16進制數,如果轉化的16進制數的長
java後端時間處理工具類,返回 "XXX 前" 的字串
我們經常會遇到顯示 "某個之間之前" 的需求(比如各種社交軟體,在回覆訊息時,顯示xxx之前回復),我們可以在後端進行處理,也可以在前端進行處理,這裡講講在後端進行處理的方法. 其實很簡單,我們只需要將從資料庫中取到的date型別的欄位進行處理。 工具類如下: import java.
Java 資料轉換/進位制轉換 工具類
package com.yunduan.parking.util; import java.math.BigInteger; import java.util.regex.Pattern; /** * Created by Administrator on 2018/8/15. */ p
java位元組字元轉換工具類
該工具類作用是位元組,字元陣列的相互轉換(避免重複造輪子),分享給大家 public class ConvertUtil { /** * 位元組陣列轉16進位制字串 &
使用java的Calendar工具類獲取到本月的第一天起始時間和最後一天結束時間。
1、使用java的Calendar工具類獲取到本月的第一天起始時間和最後一天結束時間。 1 package com.fline.aic.utils; 2 3 import java.text.DateFormat; 4 import java.text.SimpleDateFormat
java中unicode utf-8以及漢字之間的轉換工具類
1. 漢字字串與unicode之間的轉換 1.1 stringToUnicode /** * 獲取字串的unicode編碼 * 漢字“木”的Unicode 碼點為Ox6728 * * @param s 木 * @return \uf
JAVA 日期時間的工具類
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Gr
Android中日期、時間戳之間的轉換工具類
以下是我目前用到過的,後期如果遇到其他需求也會補上的,各位朋友遇到了什麼轉換也可以提示。 package com.example.zyt.myutilslist.utils; import android.util.Log; import java.text.ParseException;
java 中 byte[]、File、InputStream 互相轉換工具類
java 中 byte[]、File、InputStream 互相轉換1、將File、FileInputStream 轉換為byte陣列:File file = new File("test.txt");InputStream input = new FileInputSt