操作日期工具类

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	/**
	 * 
	 * <p>
	 * 简述:获取当天时间  str
	 * </p>
	 * <p>
	 * 描述:
	 * </p>
	 * @param dateformat "yyyy-MM-dd" "yyyy-MM-dd HH:mm:ss"
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
    public static String getNowTimeStr(String dateformat){  
        Date   now   =   new   Date();     
        //可以方便地修改日期格式
        SimpleDateFormat   dateFormat   =   new   SimpleDateFormat(dateformat);     
        String  hehe  = dateFormat.format(now);     
        return hehe;  
    } 
	/**
	 * 
	 * <p>
	 * 简述:获取当天时间 date
	 * </p>
	 * <p>
	 * 描述:
	 * </p>
	 * @param dateformat "yyyy-MM-dd" "yyyy-MM-dd HH:mm:ss"
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
    public static Date getNowTimeDate(String dateformat){  
        Date   now   =   new   Date();     
        //可以方便地修改日期格式
        SimpleDateFormat   dateFormat   =   new   SimpleDateFormat(dateformat);     
        String  hehe  = dateFormat.format(now);
        return getStrToDate(hehe, dateformat);
    }
    
	/**
	 * 
	 * <p>
	 * 简述:获取距今天的指定天数
	 * </p>
	 * <p>
	 * 描述:
	 * </p>
	 * @param index 指定天数 (今天之前的天数是负数,今天之后的是正数)
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	public static String getDayByIndex(int index) {
		Calendar c = Calendar.getInstance();
		Date date = new Date(System.currentTimeMillis());
		c.setTime(date);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day + index);
		String dayAfter = new SimpleDateFormat("yyyy-MM-dd")
				.format(c.getTime());
		return dayAfter;
	}

	/**
	 * 
	 * <p>
	 * 简述:转换日期成字符串
	 * </p>
	 * <p>
	 * 描述:
	 * </p>
	 * @param date  日期类型
	 * @param format "yyyy-MM-dd" "yyyy-MM-dd HH:mm:ss"
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	public static String getDateToStr(Date date, String format) {
		return new SimpleDateFormat(format).format(date);
	}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
日期操作工具类 /** * 将日期对象按照换为时间字符串 * 默认格式为"yyyy-MM-dd HH:mm:ss" * @param sourceTime * @return * @throws ParseException */ public static String formatDate(Date sourceDate) /** * 将日期对象按照指定格式转换为时间字符串 * @param sourceTime * @param formatStr * @return * @throws ParseException */ public static String formatDate(Date sourceDate, String formatStr) /** * 将时间字符串按照指定格式转换为时间字符串 * @param sourceTime * @param formatStr * @return * @throws ParseException */ public static String formatDate(String sourceTime, String formatStr) /** * 得到当前时间指定天数之前几天的日期 * @param days 指定的天数 * @return */ public static Date getFormerDateOfDays(int days) /** * 得到当前时间之前几个小时的日期 * @param hours 指定的小时数 * @return */ public static Date getFormerDateOfHours(int hours){} /** * 得到当前时间之前多少秒的日期 * @param sec 指定的秒数 * @return */ public static Date getFormerDateOfSecond(int sec){ } /** * 得到指定日期之前sec秒的日期 * @param days 指定的天数 * @return */ public static Date getFormerDateOfSecond(Date date, int sec){ } /** * 将时间字符串转换为日起对象 * 默认格式为“yyyy-MM-dd HH:mm:ss” * “yyyy/MM/dd HH:mm:ss”也可以 * @param sourceTime * @return * @throws ParseException */ public static Date paresDate(String sourceTime) throws ParseException { } /** * 返回指定月份的最后一天 * @param year * @param month * @return */ public static int getLastDayOfMonth(int year, int month){ } /** * 计算两个日期间的秒数 * @param beginDate * @param endDate * @return * @throws ParseException */ public static long getSecondBetweenDate(String beginDate, String endDate) throws ParseException { } /** * 将时间字符串按照指定格式转换为日期对象 * @param sourceTime * @param formatStr * @return * @throws ParseException */ public static Date paresDate(String sourceTime, String formatStr) throws ParseException { }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值