时间处理

5 篇文章 0 订阅
package jmeter;

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

public class time_function {
	// format格式为 :yyyy-MM-dd HH:mm:ss
	// 得到当前时间的时间戳
	public static long getNowStamp() throws java.text.ParseException {
		Date date = new Date();
		long currentTime = date.getTime();
		return currentTime;
	}

	// 得到String类型的当前时间
	public static String getNowString(String format) {
		Date date = new Date();
		SimpleDateFormat sf = new SimpleDateFormat(format);
		String nowDate = sf.format(date);
		return nowDate;
	}

	// 格式化时间,
	public static String getformatString(String format, String time) throws java.text.ParseException {
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		Date date = dateFormat.parse(time);
		String d = dateFormat.format(date);
		return d;
	}

	// 时间计算
	public static String dateCalculation(String date, String format, int number) throws java.text.ParseException {
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		Date dat = dateFormat.parse(date);
		Calendar cal = Calendar.getInstance();
		cal.setTime(dat);
		//1是对年份操作,2是对月份操作,3是对星期操作,5是对日期操作,11是对小时操作,12是对分钟操作,13是对秒操作,14是对毫秒操作
		cal.add(5, number);

		SimpleDateFormat sf = new SimpleDateFormat(format);
		String d = sf.format(cal.getTime());
		return d;
	}

	// 计算两个日期之间的距离
	public static int getTimeDistance(Date beginDate, Date endDate) {
		Calendar beginCalendar = Calendar.getInstance();
		beginCalendar.setTime(beginDate);
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.setTime(endDate);
		long beginTime = beginCalendar.getTime().getTime();
		long endTime = endCalendar.getTime().getTime();
		int betweenDays = (int) ((endTime - beginTime) / (1000 * 60 * 60 * 24));// 计算两个时间毫秒数之差大于一天的天数
		// 使endCalendar减去这些天数,将问题转换为两时间的毫秒数之差不足一天的情况
		endCalendar.add(Calendar.DAY_OF_MONTH, -betweenDays);
		// 再使endCalendar减去1天
		endCalendar.add(Calendar.DAY_OF_MONTH, -1);
		// 比较两日期的DAY_OF_MONTH是否相等
		if (beginCalendar.get(Calendar.DAY_OF_MONTH) == endCalendar.get(Calendar.DAY_OF_MONTH)) {
			//相等说明确实跨天了
			return betweenDays + 1;
		} else {
			//相等说明没有跨天了
			return betweenDays + 0;
		}

	}

	// String转为时间戳
	public static long stringToStamp(String time, String dateformat) throws java.text.ParseException {
		SimpleDateFormat format = new SimpleDateFormat(dateformat);
		Date date = format.parse(time);
		return date.getTime();
	}

	// 时间戳转为string
	public static String stampToString(Long stamp, String dateformat) throws java.text.ParseException {
		SimpleDateFormat format = new SimpleDateFormat(dateformat);
		String d = format.format(stamp);
		return d;
	}

	// 时间戳转为date格式
	public static Date stampToDate(Long stamp, String dateformat) throws java.text.ParseException {
		SimpleDateFormat format = new SimpleDateFormat(dateformat);
		String d = stampToString(stamp, dateformat);
		Date date = format.parse(d);
		return date;
	}

	// String转为Date
	public static Date stringToDate(String time, String dateformat) throws java.text.ParseException {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = dateFormat.parse(time);
		return date;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值