TimeUtils

package com.sinosig.app.indexcal.utils;

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

public class TimeUtils {
    /**
     * 得到当前系统日期 author: YT
     * 
     * @return 当前日期的格式字符串,日期格式为"yyyy-MM-dd"
     */
    public static String getCurrentDate() {
        String pattern = "yyyy-MM-dd";
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        Date today = new Date();
        String tString = df.format(today);
        return tString;
    }

    /**
     * 得到当前系统时间 author: YT
     * 
     * @return 当前时间的格式字符串,时间格式为"HH:mm:ss"
     */
    public static String getCurrentTime() {
        String pattern = "HH:mm:ss";
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        Date today = new Date();
        String tString = df.format(today);
        return tString;
    }

    /**
     * 
     * <p>
     * Title: monthsBetween
     * </p>
     * <p>
     * Description: 计算两个时间之间间隔月份
     * </p>
     * 
     * @param date1
     * @param date2
     * @return
     * @throws Exception
     * @author shijingshuo-ghq 2016年3月14日 上午11:25:18
     */
    public static int monthsBetween(Date date1, Date date2) throws Exception {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(date1);
        c2.setTime(date2);
        return (c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR)) * 12
                + (c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH));
    }
    /**
     * 
     * <p>Title: addMonths</p>
     * <p>Description: 月份加减</p>
     * @param yearMonth
     * @return
     * @throws Exception
     * @author shijingshuo-ghq  2017年3月24日 上午12:52:44
     */
    public static String addMonths(String yearMonth,int months) throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        Date date =  sdf.parse(yearMonth);
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, months);
        return sdf.format(c.getTime());
    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取当天日期 获取本周一日期 获取本周日的日期 获取上周一日期:" + tt.getPreviousWeekday("yyyy-MM-dd")); 获取上周日日期:" + tt.getPreviousWeekSunday("yyyy-MM-dd")); 获取上周一日期:" + tt.getWeekday(-1, "yyyy-MM-dd")); 获取上周日日期:" + tt.getWeekSunday(-1, "yyyy-MM-dd")); 获取下周一日期:" + tt.getNextMonday("yyyy-MM-dd")); 获取下周日日期:" + tt.getNextSunday("yyyy-MM-dd")); 获取本月第一天日期:" + tt.getFirstDayOfMonth()); 获取本月最后一天日期:" + tt.getDefaultDay()); 获取上月第一天日期:" + tt.getPreviousMonthFirst("yyyy-MM-dd")); 获取上月最后一天的日期:" + tt.getPreviousMonthEnd("yyyy-MM-dd")); 获取某月第一天日期:" + tt.getMonthFirst(0, "yyyy-MM-dd")); 获取某月最后一天的日期:" + tt.getMonthEnd(0, "yyyy-MM-dd")); 获取下月第一天日期:" + tt.getNextMonthFirst("yyyy-MM-dd")); 获取下月最后一天日期:" + tt.getNextMonthEnd("yyyy-MM-dd")); 获取本年的第一天日期:" + tt.getCurrentYearFirst()); 获取本年最后一天日期:" + tt.getCurrentYearEnd()); 获取去年的第一天日期:" + tt.getPreviousYearFirst()); 获取去年的最后一天日期:" + tt.getPreviousYearEnd()); 获取明年第一天日期:" + tt.getNextYearFirst()); 获取明年最后一天日期:" + tt.getNextYearEnd()); 获取本季度第一天:" + tt.getThisSeasonFirstTime(11)); 获取本季度最后一天:" + tt.getThisSeasonFinallyTime(11)); 获取两个日期之间间隔天数 获取当前月的第几周:" + tt.getWeekOfMonth()); 获取当前年份:" + tt.getYear()); 获取当前月份:" + tt.getMonth()); 获取今天在本年的第几天:" + tt.getDayOfYear()); 获得今天在本月的第几天(获得当前日):" + tt.getDayOfMonth()); 获得今天在本周的第几天:" + tt.getDayOfWeek()); 字符串转时间 获得一个日期所在周的星期几的日期
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值