CalendarUtils 日期格式化工具类

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @ClassName: CalendarUtils
 * @Description: 日历工具类:开销量较大,但是一般情况下并不会对系统造成瓶颈限制
 * @date Oct 28, 2016 5:16:24 PM
 */
public class CalendarUtils {
    
    public static final String YYYY_MM = "yyyy-MM";
    
    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    
    public static final String YYYY_MM_DD_Hms = "yyyy-MM-dd HH:mm:ss";
    
    public static final String YYYY_MM_DD_Hmss = "yyyy-MM-dd HH:mm:sss";
    
    public static final String YYYYMM = "yyyyMM";
    
    public static final String YYYYMMDD = "yyyyMMdd";
    
    public static final String YYYYMMDDHHmmss = "yyyyMMddHHmmss";
    
    public static final String YYYYMMDDHHmmsss = "yyyyMMddHHmmsss";
    
    private static final Logger logger = LoggerFactory.getLogger(CalendarUtils.class);
    
    static Calendar calendar = Calendar.getInstance();
    
    /**
     * @Title: doFormatString
     * @Description: 将时间格式化为指定格式的字符串
     * @date Oct 28, 2016 4:52:46 PM
     * @param date
     * @param type
     * @return
     * @throws ParseException
     */
    public static String doFormatString(Date date, String type) {
        String result = null;
        try {
            if (StringUtils.isNotBlank(type)) {
                SimpleDateFormat format = new SimpleDateFormat(type);
                result = format.format(date);
            } else {
                // 默认格式化为yyyy-MM-dd HH:mm:ss
                SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD_Hms);
                result = format.format(date);
            }
        } catch (Exception e) {
            logger.error("日期格式化异常.");
            e.printStackTrace();
        }
        return result;
    }
    
    /**
     * @Title: doFormatDate
     * @Description: 将指定格式的字符串格式化为时间
     * @date Oct 28, 2016 5:08:01 PM
     * @param date
     * @param type
     * @return
     * @throws ParseException
     */
    public static Date doFormatDate(String date, String type) {
        Date result = null;
        if (StringUtils.isEmpty(type)) {
            // 默认格式化为yyyy-MM-dd HH:mm:ss
            type = YYYY_MM_DD_Hms;
        }
        try {
            SimpleDateFormat format = new SimpleDateFormat(type);
            result = format.parse(date);
        } catch (ParseException e) {
            logger.error("日期格式化异常.");
            e.printStackTrace();
        }
        return result;
    }
    
    /**
     * @Title: doAddDay
     * @Description: 操作日期的增减
     * @date Oct 28, 2016 5:16:56 PM
     * @param date
     * @param n
     * @return
     */
    public static Date doAddDay(Date date, Integer n) {
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, n);
        return calendar.getTime();
    }
    
    /**
     * @Title: doAddMonth
     * @Description: 操作月份的增减
     * @date Oct 28, 2016 5:17:29 PM
     * @param date
     * @param n
     * @return
     */
    public static Date doAddMonth(Date date, Integer n) {
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, n);
        return calendar.getTime();
    }
    
    /**
     * @Title: doAddYear
     * @Description: 操作年份的增减
     * @date Oct 28, 2016 5:17:32 PM
     * @param date
     * @param n
     * @return
     */
    public static Date doAddYear(Date date, Integer n) {
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, n);
        return calendar.getTime();
    }
}
 

转载于:https://my.oschina.net/whiteInfo/blog/786414

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.hexiang.utils; import java.text.SimpleDateFormat; import java.util.*; public class CalendarUtil { public static void main(String args[]) { System.out.println("First day of week is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getFirstDateByWeek(new Date()))); System.out.println("Last day of week is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getLastDateByWeek(new Date()))); System.out.println("First day of month is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getFirstDateByMonth(new Date()))); System.out.println("Last day of month is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getLastDateByMonth(new Date()))); } /** * 获得所在星期的第一天 */ public static Date getFirstDateByWeek(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); int today = now.get(Calendar.DAY_OF_WEEK); int first_day_of_week = now.get(Calendar.DATE) + 2 - today; // 星期一 now.set(Calendar.DATE, first_day_of_week); return now.getTime(); } /** * 获得所在星期的最后一天 */ public static Date getLastDateByWeek(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); int today = now.get(Calendar.DAY_OF_WEEK); int first_day_of_week = now.get(Calendar.DATE) + 2 - today; // 星期一 int last_day_of_week = first_day_of_week + 6; // 星期日 now.set(Calendar.DATE, last_day_of_week); return now.getTime(); } /** * 获得所在月份的最后一天 * @param 当前月份所在的时间 * @return 月份的最后一天 */ public static Date getLastDateByMonth(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); now.set(Calendar.MONTH, now.get(Calendar.MONTH) + 1); now.set(Calendar.DATE, 1); now.set(Calendar.DATE, now.get(Calendar.DATE) - 1); now.set(Calendar.HOUR, 11); now.set(Calendar.MINUTE, 59); now.set(Calendar.SECOND, 59); return now.getTime(); } /** * 获得所在月份的第一天 * @param 当前月份所在的时间 * @return 月份的第一天 */ public static Date getFirstDateByMonth(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); now.set(Calendar.DATE, 0); now.set(Calendar.HOUR, 12); now.set(Calendar.MINUTE, 0); now.set(Calendar.SECOND, 0); return now.getTime(); } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值