日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

获取系统时间工具

發布時間:2025/3/18 windows 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 获取系统时间工具 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

獲取系統時間或對時間運算:

1: 獲取當前系統時間并和字符串類型進行相互轉換:

2:日期工具類:

代碼:

?

import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale;public class DateUtil {public final static String FORMAT ="yyyy-MM-dd HH:mm:ss";/*** 將Date類型轉變為指定格式的字符串*/public static String formatString(Date date, String format) {SimpleDateFormat dateFormat = new SimpleDateFormat(format, Locale.CHINA);String nowTime = dateFormat.format(date);return nowTime;}public static String formatString() {SimpleDateFormat dateFormat = new SimpleDateFormat(FORMAT, Locale.CHINA);Date date = new Date();String nowTime = dateFormat.format(date);return nowTime;}/*** 將Date類型轉變為指定格式的字符串*/public static String formatString(Date date) {SimpleDateFormat dateFormat = new SimpleDateFormat(DateUtil.FORMAT, Locale.CHINA);String nowTime = dateFormat.format(date);return nowTime;}/*** 字符串轉換成日期格式** @param date* @return* @throws ParseException*/public static Date parseDate(String date)throws ParseException {SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);Date nowTime = dateFormat.parse(date);return nowTime;}public static Date parseDate(String date,String format)throws ParseException {SimpleDateFormat dateFormat = new SimpleDateFormat(format, Locale.CHINA);Date nowTime = dateFormat.parse(date);return nowTime;}/*** 日期加減運算* @param date 對指定日期運算* @param calendarField 對日期中的那個類型進行運算,比如1表示對年運算, 5表示對天進行運算* @param amount 正數表示加,負數表示減* @return*/public static Date add(Date date, int calendarField, int amount) {if (date == null) {throw new IllegalArgumentException("The date must not be null");}Calendar c = Calendar.getInstance();c.setTime(date);c.add(calendarField, amount);return c.getTime();} View Code

?

?

?

3:日期的加減運算:

例子:

?

?4:mapper中的日期運用:

?


?

?

?

5:當前時間的時間戳轉換為字符串類型的時間:

//long timeStamp = 1495777335060;//直接是時間戳 long timeStamp = System.currentTimeMillis(); //獲取當前時間戳,也可以是你自已給的一個隨機的或是別人給你的時間戳(一定是long型的數據) SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//這個是你要轉成后的時間的格式 String sd = sdf.format(new Date(timeStamp)); // 時間戳轉換成時間System.out.println(sd);//打印出你要的時間 結果就是: 2017-05-26 13:42:15

?

6:獲取最近的一個周六:

/*** @Author * @Description //TODO 獲取當前時間的最近的下一個周六* @Date 2018/11/29 16:20* @Param * @return */public String getCurrentSturday(){SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CANADA);String format = simpleDateFormat.format(new Date());try {Date bdate = simpleDateFormat.parse(format);Calendar cal = Calendar.getInstance();cal.setTime(bdate);if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY){return format;}else {LocalDate localDate = LocalDate.parse(format);LocalDate nextSaturday = localDate.with(TemporalAdjusters.next(DayOfWeek.SATURDAY));ZoneId zone = ZoneId.systemDefault();Instant instant = nextSaturday.atStartOfDay().atZone(zone).toInstant();Date date = Date.from(instant);SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CANADA);String Saturday = dateFormat.format(date);return Saturday;}} catch (ParseException e) {e.printStackTrace();}return null;}

?

轉載于:https://www.cnblogs.com/dw3306/p/9272262.html

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的获取系统时间工具的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。