`
talentkep
  • 浏览: 100459 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

time util

阅读更多

给自己的一些工具类关于时间的判断和空对象判断

/*
 * Created on 2009-9-17
 * 
 * @author  Peter_Ke
 */
package com.peter.apps.eshidx.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author Peter_Ke
 * hleper classes 
 * call me if you want to change the code
 * Created on 2009-9-17 
 */
public class miscUtil {

    
    public static java.util.Date toDate(String dateTime) {
        // dateTime must have one space between the date and time...
        String date = dateTime.substring(0, dateTime.indexOf(" "));
        String time = dateTime.substring(dateTime.indexOf(" ") + 1);

        return toDate(date, time);
    }
    
    public static java.util.Date toDate(String date, String time) {
        if (date == null || time == null) return null;
        String month;
        String day;
        String year;
        String hour;
        String minute;
        String second;

        int dateSlash1 = date.indexOf("-");
        int dateSlash2 = date.lastIndexOf("-");

        if (dateSlash1 <= 0 || dateSlash1 == dateSlash2) return null;
        int timeColon1 = time.indexOf(":");
        int timeColon2 = time.lastIndexOf(":");
        int ms_index = time.lastIndexOf(".");

        if (timeColon1 <= 0) return null;
        year = date.substring(0, dateSlash1);
        month = date.substring(dateSlash1 + 1, dateSlash2);
        day = date.substring(dateSlash2 + 1);
        hour = time.substring(0, timeColon1);

        if (timeColon1 == timeColon2) {
            minute = time.substring(timeColon1 + 1);
            second = "0";
        } else {
            minute = time.substring(timeColon1 + 1, timeColon2);
            if(ms_index<0)
                second = time.substring(timeColon2 + 1);
            else
                second = time.substring(timeColon2 + 1 , ms_index) ;
        }

        return toDate(month, day, year, hour, minute, second);
    }
    
    public static java.util.Date toDate(int month, int day, int year, int hour, int minute, int second) {
        Calendar calendar = Calendar.getInstance();

        try {
            calendar.set(year, month - 1, day, hour, minute, second);
        } catch (Exception e) {
            return null;
        }
        return new java.util.Date(calendar.getTime().getTime());
    }
    
    public static java.util.Date toDate(String monthStr, String dayStr, String yearStr, String hourStr,
            String minuteStr, String secondStr) {
            int month, day, year, hour, minute, second;

            try {
                month = Integer.parseInt(monthStr);
                day = Integer.parseInt(dayStr);
                year = Integer.parseInt(yearStr);
                hour = Integer.parseInt(hourStr);
                minute = Integer.parseInt(minuteStr);
                second = Integer.parseInt(secondStr);
            } catch (Exception e) {
                return null;
            }
            return toDate(month, day, year, hour, minute, second);
        }
    
    public static long compareTo(Date start,Date end){
        //return -N , 0 , N for compart two Date 
        return (start.getTime() - end.getTime() )/(1000*60*60*24);
    }
    
    public static String getDateBegin(String date){
        return date+" 00:00:00";
    }
    
    public static String getDateEnd(String date){
        return date+" 23:59:59";
    }
    
    public static Date getMonthStart(Date datePatter) {
        Calendar _cal = Calendar.getInstance();
        _cal.setTime(datePatter);
        _cal.set(Calendar.DAY_OF_MONTH, 1);
        return _cal.getTime();
    }
    
    public static Date getMonthEnd(Date datePatter) {
        Calendar _cal = Calendar.getInstance();
        _cal.setTime(datePatter);
        _cal.set(Calendar.DAY_OF_MONTH, _cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        return _cal.getTime();
    }
    
    public static void printSimpleMap(Map map){
        for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
            Map.Entry element = (Map.Entry) iter.next();
            Object key = element.getKey();
            Object value = element.getValue();
            System.out.println("key=value: "+key+"="+value);
        }
    }
    
    public static Date combineDateWithTimeString(Date date,String time){
		if (date==null||time==null||time.length()==0){
			System.out.println("error of null input in combineDate");
			return null;
		}
		if (time.indexOf(':')<0){
			System.out.println("error of time input format in combineDate");
			return null;
		}
		String[] timeSplit = time.split(":");
		String hour = (String)timeSplit[0];
		String minute = (String)timeSplit[1];
		Integer h=Integer.valueOf(hour);
		Integer min=Integer.valueOf(minute);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY,h.intValue());
		calendar.set(Calendar.MINUTE,min.intValue());
		Date result = new Date(calendar.getTimeInMillis());
		return result;
	}
    
    public static List getYearList(){
        List yearList = new ArrayList();
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.MARCH,-1);
        Date today = cal.getTime();
        yearList.add(new Integer(today.getYear()+1900));
        
        cal.setTime(new Date(102,0,1));
        while(cal.getTime().getYear()<today.getYear()){
            yearList.add(new Integer(cal.getTime().getYear()+1900));
            cal.add(Calendar.YEAR,1);
        }
        return yearList;
    }
    
    public static List getMonthList(){
        List monthList = new ArrayList();
        int month;
        Date today = new Date();
        if(today.getMonth()==0){
            month=12;
        }else{
            month = today.getMonth();
        }
        monthList.add(new Integer(month));
        for (int i = 1; i <= 12; i++) {
            if(i!=month){
                monthList.add(new Integer(i));
            }
        }
        return monthList;
    }
    
    public static int getCurrentMonth(){
        Calendar c = Calendar.getInstance();     
        int month = c.get(Calendar.MONTH)+1;
        return month;
    }
    
    public static int getLastMonth(){
        Calendar c = Calendar.getInstance();     
        int month = c.get(Calendar.MONTH);
        if(month==0){
            month=12;
        }
        return month;
    }
    
    public static int getCurrentYear(){
        Calendar c = Calendar.getInstance();   
        int year = c.get(Calendar.YEAR);   
        return year;
    }
    
    public static int getLastYear(){
        return getCurrentYear()-1;
    }

    public static boolean isEmpty(Map map){
		return (map == null||map.isEmpty());
	}

	public static boolean isEmpty(String str){
		return ((str == null)||(str.trim().length() == 0));
	}

	public static boolean isEmpty(Collection c){
		return ((c == null)||(c.size() == 0));
	}

	public static boolean isEmpty(Object[] a){
		return ((a == null)||(a.length == 0));
	}
}

 

 

再来个全的time  util

/*
 * $Id: UtilDateTime.java,v 1.1 2009/04/28 06:38:23 Exp $
 *
 *  Copyright (c) 2001 The Open For Business Project - www.ofbiz.org
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a
 *  copy of this software and associated documentation files (the "Software"),
 *  to deal in the Software without restriction, including without limitation
 *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
 *  and/or sell copies of the Software, and to permit persons to whom the
 *  Software is furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included
 *  in all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 *  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 *  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
 *  OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
 *  THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package com.peter.apps.util;


import java.util.*;
import java.text.*;

/**
 * Utility class for handling java.util.Date, the java.sql data/time classes and related information
 *
 *@author     <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
 *@author     <a href="mailto:jaz@zsolv.com">Andy Zeneski</a>
 *@created    May 21, 2001
 *@version    1.1
 */
public class UtilDateTime {

    /** Return a Timestamp for right now
     * @return Timestamp for right now
     */
    public static java.sql.Timestamp nowTimestamp() {
        return new java.sql.Timestamp(System.currentTimeMillis());
    }

    /** Return a Date for right now
     * @return Date for right now
     */
    public static java.util.Date nowDate() {
        return new java.util.Date();
    }
    
    /** 
     * Return a string formatted as yyyyMMddHHmmss      
     * @return String formatted for right now
     */
    public static String nowDateString() {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        return df.format(new Date());
    }

    public static java.sql.Timestamp getDayStart(java.sql.Timestamp stamp) {
        return getDayStart(stamp, 0);
    }

    public static java.sql.Timestamp getDayStart(java.sql.Timestamp stamp, int daysLater) {
        Calendar tempCal = Calendar.getInstance();

        tempCal.setTime(new java.util.Date(stamp.getTime()));
        tempCal.set(tempCal.get(Calendar.YEAR), tempCal.get(Calendar.MONTH), tempCal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        tempCal.set(Calendar.MILLISECOND, 0);
        //if (daysLater > 0) tempCal.add(Calendar.DAY_OF_MONTH, daysLater);
        tempCal.add(Calendar.DAY_OF_MONTH, daysLater);
        return new java.sql.Timestamp(tempCal.getTime().getTime());
    }

    public static java.sql.Timestamp getNextDayStart(java.sql.Timestamp stamp) {
        return getDayStart(stamp, 1);
    }

    public static java.sql.Timestamp getDayEnd(java.sql.Timestamp stamp) {
        return getDayEnd(stamp, 0);
    }

    public static java.sql.Timestamp getDayEnd(java.sql.Timestamp stamp, int daysLater) {
        Calendar tempCal = Calendar.getInstance();

        tempCal.setTime(new java.util.Date(stamp.getTime()));
        tempCal.set(tempCal.get(Calendar.YEAR), tempCal.get(Calendar.MONTH), tempCal.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        tempCal.set(Calendar.MILLISECOND, 0);
        //if (daysLater > 0) tempCal.add(Calendar.DAY_OF_MONTH, daysLater);
        tempCal.add(Calendar.DAY_OF_MONTH, daysLater);
        return new java.sql.Timestamp(tempCal.getTime().getTime());
    }

    /** Converts a date String into a java.sql.Date
     * @param date The date String: MM/DD/YYYY
     * @return A java.sql.Date made from the date String
     */
    public static java.sql.Date toSqlDate(String date) {
        java.util.Date newDate = toDate(date, "00:00:00");

        if (newDate != null)
            return new java.sql.Date(newDate.getTime());
        else
            return null;
    }

    /** Makes a java.sql.Date from separate Strings for month, day, year
     * @param monthStr The month String
     * @param dayStr The day String
     * @param yearStr The year String
     * @return A java.sql.Date made from separate Strings for month, day, year
     */
    public static java.sql.Date toSqlDate(String monthStr, String dayStr, String yearStr) {
        java.util.Date newDate = toDate(monthStr, dayStr, yearStr, "0", "0", "0");

        if (newDate != null)
            return new java.sql.Date(newDate.getTime());
        else
            return null;
    }

    /** Makes a java.sql.Date from separate ints for month, day, year
     * @param month The month int
     * @param day The day int
     * @param year The year int
     * @return A java.sql.Date made from separate ints for month, day, year
     */
    public static java.sql.Date toSqlDate(int month, int day, int year) {
        java.util.Date newDate = toDate(month, day, year, 0, 0, 0);

        if (newDate != null)
            return new java.sql.Date(newDate.getTime());
        else
            return null;
    }

    /** Converts a time String into a java.sql.Time
     * @param time The time String: either HH:MM or HH:MM:SS
     * @return A java.sql.Time made from the time String
     */
    public static java.sql.Time toSqlTime(String time) {
        java.util.Date newDate = toDate("1/1/1970", time);

        if (newDate != null)
            return new java.sql.Time(newDate.getTime());
        else
            return null;
    }

    /** Makes a java.sql.Time from separate Strings for hour, minute, and second.
     * @param hourStr The hour String
     * @param minuteStr The minute String
     * @param secondStr The second String
     * @return A java.sql.Time made from separate Strings for hour, minute, and second.
     */
    public static java.sql.Time toSqlTime(String hourStr, String minuteStr, String secondStr) {
        java.util.Date newDate = toDate("0", "0", "0", hourStr, minuteStr, secondStr);

        if (newDate != null)
            return new java.sql.Time(newDate.getTime());
        else
            return null;
    }

    /** Makes a java.sql.Time from separate ints for hour, minute, and second.
     * @param hour The hour int
     * @param minute The minute int
     * @param second The second int
     * @return A java.sql.Time made from separate ints for hour, minute, and second.
     */
    public static java.sql.Time toSqlTime(int hour, int minute, int second) {
        java.util.Date newDate = toDate(0, 0, 0, hour, minute, second);

        if (newDate != null)
            return new java.sql.Time(newDate.getTime());
        else
            return null;
    }

    /** Converts a date and time String into a Timestamp
     * @param dateTime A combined data and time string in the format "MM/DD/YYYY HH:MM:SS", the seconds are optional
     * @return The corresponding Timestamp
     */
    public static java.sql.Timestamp toTimestamp(String dateTime) {
        java.util.Date newDate = toDate(dateTime);

        if (newDate != null)
            return new java.sql.Timestamp(newDate.getTime());
        else
            return null;
    }

    /** Converts a date String and a time String into a Timestamp
     * @param date The date String: MM/DD/YYYY
     * @param time The time String: either HH:MM or HH:MM:SS
     * @return A Timestamp made from the date and time Strings
     */
    public static java.sql.Timestamp toTimestamp(String date, String time) {
        java.util.Date newDate = toDate(date, time);

        if (newDate != null)
            return new java.sql.Timestamp(newDate.getTime());
        else
            return null;
    }

    /** Makes a Timestamp from separate Strings for month, day, year, hour, minute, and second.
     * @param monthStr The month String
     * @param dayStr The day String
     * @param yearStr The year String
     * @param hourStr The hour String
     * @param minuteStr The minute String
     * @param secondStr The second String
     * @return A Timestamp made from separate Strings for month, day, year, hour, minute, and second.
     */
    public static java.sql.Timestamp toTimestamp(String monthStr, String dayStr, String yearStr, String hourStr,
        String minuteStr, String secondStr) {
        java.util.Date newDate = toDate(monthStr, dayStr, yearStr, hourStr, minuteStr, secondStr);

        if (newDate != null)
            return new java.sql.Timestamp(newDate.getTime());
        else
            return null;
    }

    /** Makes a Timestamp from separate ints for month, day, year, hour, minute, and second.
     * @param month The month int
     * @param day The day int
     * @param year The year int
     * @param hour The hour int
     * @param minute The minute int
     * @param second The second int
     * @return A Timestamp made from separate ints for month, day, year, hour, minute, and second.
     */
    public static java.sql.Timestamp toTimestamp(int month, int day, int year, int hour, int minute, int second) {
        java.util.Date newDate = toDate(month, day, year, hour, minute, second);

        if (newDate != null)
            return new java.sql.Timestamp(newDate.getTime());
        else
            return null;
    }

    /** Converts a date and time String into a Date
     * @param dateTime A combined data and time string in the format "MM/DD/YYYY HH:MM:SS", the seconds are optional
     * @return The corresponding Date
     */
    public static java.util.Date toDate(String dateTime) {
        // dateTime must have one space between the date and time...
        String date = dateTime.substring(0, dateTime.indexOf(" "));
        String time = dateTime.substring(dateTime.indexOf(" ") + 1);

        return toDate(date, time);
    }

    /** Converts a date String and a time String into a Date
     * @param date The date String: YYYY-MM-DD
     * @param time The time String: either HH:MM or HH:MM:SS or HH:MM:SS.s
     * @return A Date made from the date and time Strings
     */
    public static java.util.Date toDate(String date, String time) {
        if (date == null || time == null) return null;
        String month;
        String day;
        String year;
        String hour;
        String minute;
        String second;

        int dateSlash1 = date.indexOf("-");
        int dateSlash2 = date.lastIndexOf("-");

        if (dateSlash1 <= 0 || dateSlash1 == dateSlash2) return null;
        int timeColon1 = time.indexOf(":");
        int timeColon2 = time.lastIndexOf(":");
        int ms_index = time.lastIndexOf(".");

        if (timeColon1 <= 0) return null;
        year = date.substring(0, dateSlash1);
        month = date.substring(dateSlash1 + 1, dateSlash2);
        day = date.substring(dateSlash2 + 1);
        hour = time.substring(0, timeColon1);

        if (timeColon1 == timeColon2) {
            minute = time.substring(timeColon1 + 1);
            second = "0";
        } else {
            minute = time.substring(timeColon1 + 1, timeColon2);
            if(ms_index<0)
                second = time.substring(timeColon2 + 1);
            else
                second = time.substring(timeColon2 + 1 , ms_index) ;
        }

        return toDate(month, day, year, hour, minute, second);
    }

    /** Makes a Date from separate Strings for month, day, year, hour, minute, and second.
     * @param monthStr The month String
     * @param dayStr The day String
     * @param yearStr The year String
     * @param hourStr The hour String
     * @param minuteStr The minute String
     * @param secondStr The second String
     * @return A Date made from separate Strings for month, day, year, hour, minute, and second.
     */
    public static java.util.Date toDate(String monthStr, String dayStr, String yearStr, String hourStr,
        String minuteStr, String secondStr) {
        int month, day, year, hour, minute, second;

        try {
            month = Integer.parseInt(monthStr);
            day = Integer.parseInt(dayStr);
            year = Integer.parseInt(yearStr);
            hour = Integer.parseInt(hourStr);
            minute = Integer.parseInt(minuteStr);
            second = Integer.parseInt(secondStr);
        } catch (Exception e) {
            return null;
        }
        return toDate(month, day, year, hour, minute, second);
    }

    /** Makes a Date from separate ints for month, day, year, hour, minute, and second.
     * @param month The month int
     * @param day The day int
     * @param year The year int
     * @param hour The hour int
     * @param minute The minute int
     * @param second The second int
     * @return A Date made from separate ints for month, day, year, hour, minute, and second.
     */
    public static java.util.Date toDate(int month, int day, int year, int hour, int minute, int second) {
        Calendar calendar = Calendar.getInstance();

        try {
            calendar.set(year, month - 1, day, hour, minute, second);
        } catch (Exception e) {
            return null;
        }
        return new java.util.Date(calendar.getTime().getTime());
    }

    /** Makes a date String in the format MM/DD/YYYY from a Date
     * @param date The Date
     * @return A date String in the format MM/DD/YYYY
     */
    public static String toDateString(java.util.Date date) {
        if (date == null) return "";
        Calendar calendar = Calendar.getInstance();

        calendar.setTime(date);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int year = calendar.get(Calendar.YEAR);
        String monthStr;
        String dayStr;
        String yearStr;

        if (month < 10) {
            monthStr = "0" + month;
        } else {
            monthStr = "" + month;
        }
        if (day < 10) {
            dayStr = "0" + day;
        } else {
            dayStr = "" + day;
        }
        yearStr = "" + year;
        //Modified by Vanish -- 2003.1.2
        return yearStr + "-" + monthStr + "-" + dayStr ;
//        return monthStr + "/" + dayStr + "/" + yearStr;
    }

    /** Makes a time String in the format HH:MM:SS from a Date. If the seconds are 0, then the output is in HH:MM.
     * @param date The Date
     * @return A time String in the format HH:MM:SS or HH:MM
     */
    public static String toTimeString(java.util.Date date) {
        if (date == null) return "";
        Calendar calendar = Calendar.getInstance();

        calendar.setTime(date);
        return (toTimeString(calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND)));
    }

    /** Makes a time String in the format HH:MM:SS from a separate ints for hour, minute, and second. If the seconds are 0, then the output is in HH:MM.
     * @param hour The hour int
     * @param minute The minute int
     * @param second The second int
     * @return A time String in the format HH:MM:SS or HH:MM
     */
    public static String toTimeString(int hour, int minute, int second) {
        String hourStr;
        String minuteStr;
        String secondStr;

        if (hour < 10) {
            hourStr = "0" + hour;
        } else {
            hourStr = "" + hour;
        }
        if (minute < 10) {
            minuteStr = "0" + minute;
        } else {
            minuteStr = "" + minute;
        }
        if (second < 10) {
            secondStr = "0" + second;
        } else {
            secondStr = "" + second;
        }
        if (second == 0)
            return hourStr + ":" + minuteStr;
        else
            return hourStr + ":" + minuteStr + ":" + secondStr;
    }

    /** Makes a combined data and time string in the format "MM/DD/YYYY HH:MM:SS" from a Date. If the seconds are 0 they are left off.
     * @param date The Date
     * @return A combined data and time string in the format "MM/DD/YYYY HH:MM:SS" where the seconds are left off if they are 0.
     */
    public static String toDateTimeString(java.util.Date date) {
        if (date == null) return "";
        String dateString = toDateString(date);
        String timeString = toTimeString(date);

        if (dateString != null && timeString != null)
            return dateString + " " + timeString;
        else
            return "";
    }

    /** Makes a Timestamp for the beginning of the month
     * @return A Timestamp of the beginning of the month
     */
    public static java.sql.Timestamp monthBegin() {
        Calendar mth = Calendar.getInstance();

        mth.set(Calendar.DAY_OF_MONTH, 1);
        mth.set(Calendar.HOUR_OF_DAY, 0);
        mth.set(Calendar.MINUTE, 0);
        mth.set(Calendar.SECOND, 0);
        mth.set(Calendar.AM_PM, Calendar.AM);
        return new java.sql.Timestamp(mth.getTime().getTime());
    }

    public static java.sql.Timestamp getDayBegin(String date) throws ParseException{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        java.util.Date fieldDate = sdf.parse(date);

        return new java.sql.Timestamp(fieldDate.getTime());
    }

    public static java.sql.Timestamp getDayEnd(String date) throws ParseException{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        java.util.Date fieldDate = sdf.parse(date);

        return getDayEnd(new java.sql.Timestamp(fieldDate.getTime()));

    }
    
    public static long compareTo(Date start,Date end){
        //return -N , 0 , N for compart two Date 
        return (start.getTime() - end.getTime() )/(1000*60*60*24);
    }
    
    public static String getDateBegin(String date){
        return date+" 00:00:00";
    }
    
    public static String getDateEnd(String date){
        return date+" 23:59:59";
    }
    
    public static Date getBeforeDay(Date date,int days){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE,days);
        Date alertTime = cal.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        java.util.Date fieldDate = null;
        try {
            fieldDate = sdf.parse(alertTime.toLocaleString());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return fieldDate;
    }
    
    public static int get(Date startDate,Date endDate){
        return (int)((startDate.getTime()-endDate.getTime())/(60*1000));
    }
    
    public static Date getStartDayOfWeek(Date datePatter) {
        Calendar _cal = Calendar.getInstance();
        _cal.setTime(datePatter);
        if (_cal.get(_cal.DAY_OF_WEEK) == _cal.SUNDAY) {
            _cal.add(_cal.DAY_OF_YEAR, -6);
            return _cal.getTime();
        } else if(_cal.get(_cal.DAY_OF_WEEK) == _cal.MONDAY) {
            return datePatter;
        }else  { 
            _cal.add(_cal.DAY_OF_YEAR, (_cal.MONDAY - _cal.get(_cal.DAY_OF_WEEK)));
            return _cal.getTime();
        }
    }
    
    public static Date getEndDayOfWeek(Date datePatter) {
        Calendar _cal = Calendar.getInstance();
        _cal.setTime(datePatter);
        if (_cal.get(_cal.DAY_OF_WEEK) == _cal.SUNDAY) {
            return datePatter;
        } else {
            _cal.add(_cal.DAY_OF_YEAR, (8 - _cal.get(_cal.DAY_OF_WEEK)));
            return _cal.getTime();
        }
    }
    
    public static Date getMonthStart(Date datePatter) {
        Calendar _cal = Calendar.getInstance();
        _cal.setTime(datePatter);
        _cal.set(_cal.DAY_OF_MONTH, 1);
        return _cal.getTime();
    }
    
    public static Date getMonthEnd(Date datePatter) {
        Calendar _cal = Calendar.getInstance();
        _cal.setTime(datePatter);
        _cal.set(_cal.DAY_OF_MONTH, _cal.getActualMaximum(_cal.DAY_OF_MONTH));
        return _cal.getTime();
    }
}

 

 

分享到:
评论

相关推荐

    java.util.TimeZone 的世界时区列表

    ### Java.util.TimeZone的世界时区列表解析 #### 一、概述 `java.util.TimeZone` 是Java标准库中的一个类,用于表示不同时区的信息。本文档提供了通过`java.util.TimeZone`类导出的世界范围内时区列表。该列表不仅...

    util-linux-ng-2.17源码(含fdisk)

    util-linux-ng-2.17源码(含fdisk) Here is a list of all documented files with brief descriptions: util-linux-ng-2.17.2/disk-utils/blockdev.c [code] util-linux-ng-2.17.2/disk-utils/cramfs.h [code] ...

    C# Util 实用工具类

    3. **Time**: 时间处理工具类帮助开发者进行日期和时间的格式化、比较、计算等操作。例如,可以提供方便的时间戳转换、日期格式化函数,以及检查时间间隔的辅助方法。 4. **Compress**: 压缩和解压缩是常见的数据...

    java Util

    - `java.time`模块(Java 8及以上):引入了`LocalDate`, `LocalTime`, `LocalDateTime`, `ZonedDateTime`等新类,提供更强大和直观的日期时间处理。 4. **IO流**: - `InputStream` 和 `OutputStream`:处理字节...

    java.util.pdf

    在java.util包中,包含了一些Java集合框架的基础类,比如Enumeration、Hashtable、Stack、Vector等,以及日期和时间处理相关的类,如Calendar、Date、TimeZone等。同时,提及了Timer和TimerTask,它们用于任务调度,...

    java.util包

    在Java 8之后,新的日期和时间API(java.time包)提供了更好的日期和时间处理能力,但旧的Date和Calendar仍然被广泛使用。 8. Collections和Arrays工具类:Collections提供了许多静态方法,用于操作集合,如排序、...

    util包

    - **java.time**子包:自Java 8引入,包含更现代、强大且易于使用的日期时间API,如LocalDate、LocalTime、LocalDateTime和ZonedDateTime。 4. **迭代器**: `util`包定义了`Iterator`接口,它是遍历集合元素的...

    项目中可能会用到的Util类 如 Date String

    Java 8引入了新的日期时间API (`java.time`包),提供了更加简洁和人性化的接口,如`LocalDate`, `LocalTime`, `LocalDateTime`, `DateTimeFormatter`等。Date Util通常会封装这些API,提供更易于使用的静态方法,...

    java_util 工具包

    然而,随着JDK版本的演进,`Date`类的部分方法已被标记为过时,推荐使用更为现代的`java.time`包中的类如`LocalDate`, `LocalTime`, 和`ZonedDateTime`等进行日期和时间的处理。 ##### 构造函数详解 1. **无参数...

    java程序设计 UTIL包.docx

    Java程序设计中的UTIL包是Java开发中非常关键的一部分,...同时,UTIL包中的其他工具类,如Date和Time类,以及字符串操作类,都是Java开发中不可或缺的部分。总的来说,理解和掌握UTIL包是提升Java编程能力的重要步骤。

    java常用util类

    在Java 8之后,推荐使用`java.time`包中的`LocalDate`、`LocalTime`、`LocalDateTime`等类。 - `Calendar`是抽象类,提供了更灵活的日期时间操作,如日历字段的增加和减少,但使用相对复杂。 7. **功能接口**: ...

    java.util包总结

    8. TimeZone和Locale类:处理时区和地域设置,用于国际化和本地化。 9. EventObject和EventListener接口:与事件处理相关,是观察者模式的一部分,通常在图形用户界面编程中使用。 10. ResourceBundle类:用于加载...

    java-util大全.rar

    Java的`java.util.Date`和`java.time`包提供了处理日期和时间的方法。`SimpleDateFormat`用于格式化和解析日期,而Java 8引入的`LocalDate`, `LocalTime`, `LocalDateTime`等类提供了更强大、更易用的时间日期API。...

    java-util包资料

    - **Java 8后的新日期时间API**:`java.time`包下的`LocalDate`, `LocalTime`, `LocalDateTime`, `ZonedDateTime`等,提供了更强大、更易用的日期和时间处理功能。 4. **泛型**: - 自Java 5引入,提高了类型安全...

    Java Date Time教程-java.util.Date

    Java的java.util.Date类是Java初的时间类之一。该类的大部分方法已不推荐使用,取而代之的是java.util.Calendar类。不过你仍然可以使用java.util.Date类去表示某个时间。下面是一个如何实例化java.util.Date的例子:...

    TimeIsLife.zip_time.is精准时间_time.is精确时间_倒计时java_精确时间time.is

    在Java中,处理时间的基本类是`java.util.Date`和`java.util.Calendar`,但这两个类自Java 8以后已被新的`java.time`包中的类所取代,如`LocalDate`, `LocalTime`, `LocalDateTime`, `Instant`等。这些新类提供了...

    Java util

    但在Java 8之后,已经被`java.time`包中的类所替代。 - `Calendar`类:抽象基类,提供了日期和时间字段的计算,如月份、日期、小时等。 - `SimpleDateFormat`:日期和时间格式化,允许自定义日期时间的字符串表示...

    java util 类

    `Date`和`Calendar`是处理日期和时间的类,但在Java 8之后,`java.time`包的`LocalDate`、`LocalTime`、`LocalDateTime`等类更推荐使用,它们提供了更加直观和灵活的时间操作。 `Random`类用于生成随机数,可以生成...

    joda-time源码

    Joda-Time考虑了闰秒的存在,这是`java.util.Date`和`Calendar`所忽视的。 6. **日期时间的查询** Joda-Time提供强大的日期时间查询功能,例如找出一年中的最后一天、获取当前季度的第一天等。 7. **兼容性与互...

    java并发工具包 java.util.concurrent中文版pdf

    ### Java并发工具包 `java.util.concurrent` 知识点详解 #### 一、引言 随着多核处理器的普及和应用程序复杂度的增加,多线程编程成为了现代软件开发不可或缺的一部分。为了简化并发编程的复杂性,Java 5 引入了 `...

Global site tag (gtag.js) - Google Analytics