java擷取目前時間,本周第一和最後一天,本月第一和最後一天,工具類

來源:互聯網
上載者:User

標籤:ken   pac   sys   設定   第一天   end   desc   ons   pre   

package com.banksteel.openerp.commons.utils;import java.text.SimpleDateFormat;import java.util.Calendar;public class CalendarUtils{/** * @description:擷取當前的時間 * @param pattern 時間格式 * @return 當前的時間 * @author: * @createTime:2016年12月13日 下午4:36:32 */public static String getNowTime(String pattern){// 當周開始時間Calendar currentDate = Calendar.getInstance();SimpleDateFormat format = new SimpleDateFormat(pattern);if (pattern == null || pattern.equals("")){pattern = "yyyy-MM-dd";}return format.format(currentDate.getTime());}/** * @description:擷取本周的開始時間 * @param pattern 時間格式 * @return 本周的開始時間 * @author: * @createTime:2016年12月13日 下午4:28:12 */public static String getWeekStart(String pattern){// 當周開始時間Calendar currentDate = Calendar.getInstance();currentDate.setFirstDayOfWeek(Calendar.MONDAY);currentDate.set(Calendar.HOUR_OF_DAY, 0);currentDate.set(Calendar.MINUTE, 0);currentDate.set(Calendar.SECOND, 0);currentDate.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);SimpleDateFormat format = new SimpleDateFormat(pattern);if (pattern == null || pattern.equals("")){pattern = "yyyy-MM-dd";}return format.format(currentDate.getTime());}/** * @description:擷取本周的結束時間 * @param pattern 時間格式 * @return 本周的結束時間 * @author: * @createTime:2016年12月13日 下午4:28:58 */public static String getWeekEnd(String pattern){// 當周結束時間Calendar currentDate = Calendar.getInstance();currentDate.setFirstDayOfWeek(Calendar.MONDAY);currentDate.set(Calendar.HOUR_OF_DAY, 23);currentDate.set(Calendar.MINUTE, 59);currentDate.set(Calendar.SECOND, 59);currentDate.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);SimpleDateFormat format = new SimpleDateFormat(pattern);if (pattern == null || pattern.equals("")){pattern = "yyyy-MM-dd";}return format.format(currentDate.getTime());}/** * @description:擷取當前月第一天 * @param pattern 時間格式 * @return 當前月第一天 * @author: * @createTime:2016年12月13日 下午4:28:12 */public static String getMonthStart(String pattern){//擷取當前月第一天Calendar currentDate = Calendar.getInstance();   currentDate.add(Calendar.MONTH, 0);currentDate.set(Calendar.DAY_OF_MONTH,1);//設定為1號,當前日期既為本月第一天 if (pattern == null || pattern.equals("")){pattern = "yyyy-MM-dd";}SimpleDateFormat format = new SimpleDateFormat(pattern);return format.format(currentDate.getTime());}/** * @description:擷取當前月最後一天 * @param pattern 時間格式 * @return 當前月最後一天 * @author: * @createTime:2016年12月13日 下午4:28:58 */public static String getMonthEnd(String pattern){//擷取當前月最後一天Calendar currentDate = Calendar.getInstance();  currentDate.set(Calendar.DAY_OF_MONTH, currentDate.getActualMaximum(Calendar.DAY_OF_MONTH));  if (pattern == null || pattern.equals("")){pattern = "yyyy-MM-dd";}SimpleDateFormat format = new SimpleDateFormat(pattern);return format.format(currentDate.getTime());}public static void main(String[] args){//System.out.println("當前的時間:" +getNowTime("yyyy-MM-dd"));//System.out.println("本周第一天:" +getWeekStart("yyyy-MM-dd"));//System.out.println("本周最後一天:" +getWeekEnd("yyyy-MM-dd"));System.out.println("當前月第一天:" +getMonthStart("yyyy-MM-dd"));System.out.println("當前月最後一天:" +getMonthEnd("yyyy-MM-dd"));}}

  

java擷取目前時間,本周第一和最後一天,本月第一和最後一天,工具類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.