java/javascript 時間操作工具類

一、java 時間操作工具類

import org.springframework.util.StringUtils;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;/*** 時間操作工具類** @author zwq**/
public class TimeFrameUtil {/*** 獲取今天的時間范圍* @return 返回長度為2的字符串集合,如:[2017-11-03 00:00:00, 2017-11-03 24:00:00]*/public static List<String> getTodayRange() {List<String> dataList = new ArrayList<>(2);SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");Calendar calendar = Calendar.getInstance();calendar.setTimeInMillis(System.currentTimeMillis());calendar.add(Calendar.DATE, 0);String today = dateFormat.format(calendar.getTime());dataList.add(today + " 00:00:00");dataList.add(today + " 24:00:00");return dataList;}/*** 獲取昨天的時間范圍* @return 返回長度為2的字符串集合,如:[2017-11-02 00:00:00, 2017-11-02 24:00:00]*/public static List<String> getYesterdayRange() {List<String> dataList = new ArrayList<>(2);SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");Calendar calendar = Calendar.getInstance();calendar.setTimeInMillis(System.currentTimeMillis());calendar.add(Calendar.DATE, -1);String yesterday = dateFormat.format(calendar.getTime());dataList.add(yesterday + " 00:00:00");dataList.add(yesterday + " 24:00:00");return dataList;}/*** 獲取本周的時間范圍* @return 返回長度為2的字符串集合,如:[2017-10-30 00:00:00, 2017-11-05 24:00:00]*/public static List<String> getCurrentWeekRange() {List<String> dataList = new ArrayList<>(2);SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");Calendar calendar = Calendar.getInstance();calendar.setFirstDayOfWeek(Calendar.MONDAY);//設置周一為一周之內的第一天
        calendar.setTimeInMillis(System.currentTimeMillis());calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);String monday = dateFormat.format(calendar.getTime()) + " 00:00:00";calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);String sunday = dateFormat.format(calendar.getTime()) + " 24:00:00";dataList.add(monday);dataList.add(sunday);return dataList;}/*** 獲取本周的時間范圍(不帶時分秒)* @return 返回長度為2的字符串集合,如:[2017-10-30, 2017-11-05]*/public static List<String> getCurrentWeekRangeNoTime() {List<String> dataList = new ArrayList<>(2);SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");Calendar calendar = Calendar.getInstance();calendar.setFirstDayOfWeek(Calendar.MONDAY);//設置周一為一周之內的第一天
        calendar.setTimeInMillis(System.currentTimeMillis());calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);String monday = dateFormat.format(calendar.getTime());calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);String sunday = dateFormat.format(calendar.getTime());dataList.add(monday);dataList.add(sunday);return dataList;}/*** 獲取本月的時間范圍* @return 返回長度為2的字符串集合,如:[2017-11-01 00:00:00, 2017-11-30 24:00:00]*/public static List<String> getCurrentMonthRange() {List<String> dataList = new ArrayList<>(2);SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");Calendar calendar = Calendar.getInstance();calendar.setTimeInMillis(System.currentTimeMillis());calendar.add(Calendar.MONTH, 0);calendar.set(Calendar.DAY_OF_MONTH, 1);String firstDayOfMonth = dateFormat.format(calendar.getTime()) + " 00:00:00";calendar.add(Calendar.MONTH, 1);calendar.set(Calendar.DAY_OF_MONTH, 0);String lastDayOfMonth = dateFormat.format(calendar.getTime()) + " 24:00:00";dataList.add(firstDayOfMonth);dataList.add(lastDayOfMonth);return dataList;}/*** 獲取本年的時間范圍* @return 返回長度為2的字符串集合,如:[2017-01-01 00:00:00, 2017-12-31 24:00:00]*/public static List<String> getCurrentYearRange() {List<String> dataList = new ArrayList<>(2);SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");Calendar calendar = Calendar.getInstance();calendar.setTimeInMillis(System.currentTimeMillis());calendar.add(Calendar.YEAR, 0);calendar.set(Calendar.DAY_OF_YEAR, 1);String firstDayOfYear = dateFormat.format(calendar.getTime()) + " 00:00:00";calendar.add(Calendar.YEAR, 1);calendar.set(Calendar.DAY_OF_YEAR, 0);String lastDayOfYear = dateFormat.format(calendar.getTime()) + " 24:00:00";dataList.add(firstDayOfYear);dataList.add(lastDayOfYear);return dataList;}/*** 獲取最近幾天的時間范圍* @param lastFewDays 最近多少天* @return 返回長度為2的字符串集合,如:[2017-12-25 17:15:33, 2017-12-26 17:15:33]*/public static List<String> getLastFewDaysRange(int lastFewDays) {List<String> dataList = new ArrayList<>(2);SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Calendar calendar = Calendar.getInstance();calendar.setTimeInMillis(System.currentTimeMillis());String endTime = dateFormat.format(calendar.getTime());calendar.add(Calendar.DATE, -lastFewDays);String startTime = dateFormat.format(calendar.getTime());dataList.add(startTime);dataList.add(endTime);return dataList;}/*** 獲取當前時間* @param pattern 指定返回當前時間的格式,例:"yyyy-MM-dd HH:mm:ss"* @return 返回指定格式的當前時間,如:"2018-01-25 10:14:30"*/public static String getCurrentTime(String pattern) {String currentTime;SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);Calendar calendar = Calendar.getInstance();calendar.setTimeInMillis(System.currentTimeMillis());currentTime = dateFormat.format(calendar.getTime());return currentTime;}/**** 將指定時間偏移幾小時* @param time 指定時間,精確到分,例:"2018-01-25 10:48"* @param offset 偏移量:負數代表減幾個小時,正數代表加幾個小時,例:1* @return 返回偏移后的時間,如:"2018-01-25 11:48"* @throws ParseException*/public static String offsetHours(String time, int offset) throws ParseException {String offsetHours = null;if (StringUtils.hasText(time)) {SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");Calendar calendar = Calendar.getInstance();calendar.setTime(dateFormat.parse(time));calendar.add(Calendar.MINUTE, offset);offsetHours = dateFormat.format(calendar.getTime());}return offsetHours;}/*** 將指定月份偏移幾個月* @param month 指定月份* @param offset 偏移量:負數代表上幾個月,正數代表下幾個月* @return 返回偏移后的月份,如:2018-01* @throws ParseException*/public static String offsetMonths(String month, int offset) throws ParseException {String offsetMonth = null;if (StringUtils.hasText(month)) {SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");Calendar calendar = Calendar.getInstance();calendar.setTime(dateFormat.parse(month));calendar.add(Calendar.MONTH, offset);offsetMonth = dateFormat.format(calendar.getTime());}return offsetMonth;}/*** 獲取指定日期是星期幾(設置星期一為一周的開始)* @param day 指定日期* @return 返回星期幾,如:1* @throws ParseException*/public static int getDayOfWeek(String day) throws ParseException {int dayOfWeek = 0;if (StringUtils.hasText(day)) {SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");Calendar calendar = Calendar.getInstance();calendar.setTime(dateFormat.parse(day));calendar.add(Calendar.DATE, -1); //在指定日期的基礎上減一天,滿足中國人的習慣dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);}return dayOfWeek;}/*** 獲取指定月份有多少天* @param month 指定月份* @return 返回天數,如:31* @throws ParseException*/public static int getDaysInMonth(String month) throws ParseException {SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");Calendar calendar = Calendar.getInstance();calendar.setTime(dateFormat.parse(month));calendar.add(Calendar.MONTH, 1);calendar.set(Calendar.DAY_OF_MONTH, 0);return calendar.get(Calendar.DAY_OF_MONTH);}}

二、javascript 時間操作工具類

/*** 時間操作工具類** @author zwq**/
var TimeFrameUtil = {/*** 格式化日期* @param date {Date} 日期* @param pattern {string} 格式,例:"yyyy-MM-dd HH:mm:ss"* @returns {String} 返回格式化后的日期,如:"2018-01-22 18:04:30"*/format : function (date, pattern) {var time = {"M+": date.getMonth() + 1,"d+": date.getDate(),"H+": date.getHours(),"m+": date.getMinutes(),"s+": date.getSeconds(),"q+": Math.floor((date.getMonth() + 3) / 3),"S+": date.getMilliseconds()};if (/(y+)/i.test(pattern)) {pattern = pattern.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));}for (var k in time) {if (new RegExp("(" + k + ")").test(pattern)) {pattern = pattern.replace(RegExp.$1, RegExp.$1.length == 1 ? time[k] : ("00" + time[k]).substr(("" + time[k]).length));}}return pattern;},/*** 將指定時間偏移幾小時* @param time {String} 指定時間,例:"2018-01-24 17:00"* @param offset {Number} 偏移量,正數代表加幾小時,負數代表減幾小時,例:1* @param pattern {String} 返回時間的格式,例:"yyyy-MM-dd HH:mm"* @returns {String} 返回計算后的時間,如:"2018-01-24 18:00"*/offsetHours : function (time, offset, pattern) {var date = new Date(Date.parse(time));var yyyy = date.getFullYear();var MM = date.getMonth();var dd = date.getDate();var HH = date.getHours() + offset;var mm = date.getMinutes();var ss = date.getSeconds();return this.format(new Date(yyyy, MM, dd, HH, mm, ss), pattern);},/*** 將指定月份偏移幾個月* @param month {String} 指定月份,例:"2018-01"* @param offset {Number} 偏移量,負數代表上幾個月,正數代表下幾個月,例:1* @returns {String} 返回計算后的月份,如:"2018-02"*/offsetMonths : function (month, offset) {var date = new Date(Date.parse(month));var year = date.getFullYear();var month = date.getMonth();var preOrNextMonth = month + offset;return this.format(new Date(year, preOrNextMonth), "yyyy-MM");},/*** 獲取指定日期是星期幾* @param date {String} 指定日期,例:"2018-01-23"* @returns {Number} 返回星期幾(1-7),如:2*/dayOfWeek : function (date) {var time = new Date(Date.parse(date));var weekday=new Array(7);weekday[0]= 7;weekday[1]= 1;weekday[2]= 2;weekday[3]= 3;weekday[4]= 4;weekday[5]= 5;weekday[6]= 6;return weekday[time.getDay()];},/*** 獲取指定月份有多少天* @param month {String} 指定月份:例"2018-01"* @returns {number} 返回指定月份有多少天,如:31*/daysInMonth : function (month) {var date = new Date(Date.parse(month));var year = date.getFullYear();var month = date.getMonth();if (month == 1) {if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)return 29;elsereturn 28;} else if ((month <= 6 && month % 2 == 0) || (month > 6 && month % 2 == 1))return 31;elsereturn 30;}
};

?

轉載于:https://www.cnblogs.com/zhuwenqi2016/p/7779648.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/540380.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540380.shtml
英文地址,請注明出處:http://en.pswp.cn/news/540380.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

[轉載] java語言程序設計-基礎篇

參考鏈接&#xff1a; Java中的決策制定(if&#xff0c;if-else&#xff0c;switch&#xff0c;break&#xff0c;continue&#xff0c;jump) 第1章&#xff0c;計算機、程序和Java概述 包括【每個java初學者都應該搞懂的問題】 http://blog.csdn.net/haobo920/article/detai…

Exchange server 2013(十四)WSUS部署及組策略設置(2)

我們繼續上一節未完的博客&#xff0c;繼續我們的WSUS設置。[上一章節標題&#xff1a;Exchange server 2013(十四)WSUS部署及組策略設置(1) 網址&#xff1a;http://1183839.blog.51cto.com/blog/1173839/1182366] 首先單擊自動審批,來修改審批規則,也就是說當wsus偵測到新的更…

[轉載] Python中的switch語句的替代品

參考鏈接&#xff1a; Java中的switch語句 Python 中沒有 switch語句。 一般用if-else 語句可以替代switch語句&#xff0c;今天學習了使用字典的映射來代替switch語句。Mark一下 day 8; def get_sunday(): return sunday def get_monday(): return monday def get_tues…

“□” 表情無法在 Android 設備顯示? 快試試 EmojiCompact 表情兼容庫

有了支持庫 v26 下的 Compact 庫&#xff0c;基于 API 19 及更高版本開發的應用就能獲得 emoji 的向下兼容&#xff0c;再也不會看到顯示失敗的 “豆腐塊” 啦。如要使用 EmojiCompact&#xff0c;需要在應用啟動時通過下載或者打包字體來初始化庫。通過集成小插件&#xff0c;…

[轉載] jstl獲取Parameter參數及switch使用

參考鏈接&#xff1a; Java中的字符串使用switch <% taglib uri"/WEB-INF/tld/c.tld" prefix"c" %> param 獲取 Parameter參數 <c:choose> <c:when test"${empty param.name}"> Please enter your name. …

syslinux 制作多系統啟動U盤

syslinux 制作多系統啟動U盤標簽&#xff1a;u盤使用 syslinux 制作多系統啟動U盤 syslinux 能加載虛擬內存盤&#xff0c;能引導各種鏡像文件&#xff0c;適合用于制作多系統啟動U盤&#xff0c; 但是我按照網上說的步驟&#xff0c;總是不能制作成功&#xff1a; 1. 格式化…

[轉載] JAVA基礎----java中E,T,?的區別?

batch_size 1 layout "NHWC" target tvm.target.Target("cuda") dtype "float32" log_file "%s-%s-B%d.json" % (network, layout, batch_size)

用MATLAB結合四種方法搜尋羅馬尼亞度假問題

選修了cs的AI課&#xff0c;開始有點不適應&#xff0c;只能用matlab硬著頭皮上了&#xff0c;不過matlab代碼全網僅此一份&#xff0c;倒有點小自豪。 一、練習題目 分別用寬度優先、深度優先、貪婪算法和 A*算法求解“羅馬利亞度假問題”。具體地圖我這里不給出了&#xff0c…

[轉載] Java中文與ASCII碼的轉換

參考鏈接&#xff1a; 擴展Java中的原始轉換 今天在研究Java中編碼的時候&#xff0c;看到了Java中ascii碼的強大。寫了一個CoderUtils.java&#xff0c;以后會擴展它。 package com.xingxd.study.test; import java.io.File; import java.io.FileWriter; import java.io.I…

[轉]Paul Adams:為社交設計

為社交設計 Strong, Weak, and Temporary Ties by Paul Adams on 2010/04/09 PS&#xff1a;作者Paul Adams Facebook全球品牌體驗總監 電話和手機聚集十億用戶用了15年的時間&#xff0c;而Facebook只用了9個月。我們看到越來越多的人開始用在線社交網絡&#xff0c;這種網絡好…

[轉載] Java中日期格式轉換

參考鏈接&#xff1a; Java中的類型轉換和示例 Code: /** * 字符串轉換為java.util.Date<br> * 支持格式為 yyyy.MM.dd G at hh:mm:ss z 如 2002-1-1 AD at 22:10:59 PSD<br> * yy/MM/dd HH:mm:ss 如 2002/1/1 17:55:00<br> * yy/MM/dd HH:…

Android Framework中的Application Framework層介紹

Android的四層架構相比大家都很清楚&#xff0c;老生常談的說一下分別為&#xff1a; Linux2.6內核層&#xff0c;核心庫層&#xff0c;應用框架層&#xff0c;應用層。我今天重點介紹一下應用框架層Framework。 Framework層為我們開發應用程序提供了非常多的API&#xff0c;我…

[轉載] java注釋

參考鏈接&#xff1a; Java注釋 Java注釋 java中注釋有三種&#xff1a;這些都稱之為java doc標記&#xff0c;含義如下&#xff1a; java中注釋有三種&#xff1a; 單行注釋 //注釋的內容&#xff0c;多行注釋 /…注釋的內容…/&#xff0c;文檔注釋 /**…注釋的內容….*/。…

環路是怎樣形成的實例

環路是怎樣形成的一個由十多臺交換機組成的小型局域網&#xff0c;交換機大多是Cisco的中低端系列產品。某日突然出現問題&#xff1a;局域網內的主機之間相互ping時&#xff0c;都出現延時長、丟包現象&#xff0c;網絡應用奇慢無比。 觀察交換機設備&#xff0c;指示燈看不出…

[轉載] 《Python語言程序設計》課程筆記

參考鏈接&#xff1a; Python程式設計語言 文章目錄 第一部分 Python快速入門第1周 Python基本語法元素第2周 Python基本圖形繪制 第二部分 Python基礎語法第3周 基本數據類型3.1 數字類型及操作3.3 字符串類型及操作3.4 模塊2: time庫的使用 第4周 程序的控制結構4.1 程序的分…

ORACLE中創建如何創建表,并設置結構和默認值

使用select語句查看EMP表&#xff0c;根據COMM排序 默認情況下&#xff0c;空值會自動排列在尾部。 利用nulls last排序時將空值置底 利用nulls first排序時將空值置頂 例 創建一張出版社表 使用語句 create table 表名&#xff08;列名1 類型&#xff0c;列名2 類型&#xff0…

[轉載] C++靈魂所在之---多態的前世與今生

參考鏈接&#xff1a; Java是否支持goto 開頭先送大家一句話吧&#xff1a; 眾所周知&#xff0c;在20世紀80年代早期&#xff0c;C在貝爾實驗室誕生了&#xff0c;這是一門面向對象的語言&#xff0c;但它又不是全新的面向對象的語言&#xff0c;它是在傳統的語言…

Code Sinppet

如果你在使用VS 2005,如果你不能使用它的Code Snippet功能&#xff0c;如果你在實現抽象類override 方法時彈出&#xff1a;Code Snippet titled [Method Stub - Body] failed to load. Verify that refactoring snippets are recognized in the Code Snippet Manager and that…

暴風TV請來中國人工智能first lady馮雁教授任首席科學家

今日下午&#xff0c;暴風AI無屏電視發布會現場&#xff0c;暴風TV宣布邀請號稱“中國人工智能first lady”、于香港科技大學任教的馮雁教授&#xff0c;擔任暴風TV人工智能首席科學顧問。 馮雁教授于現場表示&#xff0c;選擇暴風TV合作的重要原因&#xff0c;一方面在于其個人…

[轉載] java 計算協方差_Java的深度:通過協方差暴露的API泄漏

參考鏈接&#xff1a; 關于Java中null的有趣事實 java 計算協方差 Java有時可能非常棘手&#xff0c;特別是在API設計中。 讓我們看一個非常有趣的展示柜。 jOOQ強烈地將API與實現分開。 所有API都在org.jooq包中&#xff0c;并且是公共的。 大多數實現是在org.jooq.impl包…