日期操作

转换

  1. 获取当前时间,并转化为String类型
方法一:使用 SimpleDateFormat类:
SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
String datetime = tempDate.format(new java.util.Date());  
//也可以用这个  
// String datetime = tempDate.format(new Date(System.currentTimeMillis()));
方法二:使用 Calendar类:
Calendar   c   =   Calendar.getInstance();//可以用set()对每个时间域单独修改    
  int   year   =   c.get(Calendar.YEAR);    
//一般month都需要+1才表示当前月份  
  int   month   =   c.get(Calendar.MONTH);    
  int   date   =   c.get(Calendar.DATE);    
  int   hour   =   c.get(Calendar.HOUR_OF_DAY);    
  int   minute   =   c.get(Calendar.MINUTE);    
  int   second   =   c.get(Calendar.SECOND);    
  String nowTime = year + "/" + month + "/" + date + "   " +hour + ":" + minute + ":" + second;
  1. 毫秒与日期间的转换
//时间戳转日期
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr = dateformat.format(System.currentTimeMillis());

//日期转时间戳
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");  
//加上毫秒设置可确保精度,解决转换前后毫秒数不一致问题
long time = dateformat.parse("2016-09-02 23:02:17").getTime();
  1. 时间戳转Date

Date oldDate = new Date(registTime) ;

  1. 时间戳,String,Date之间转换
SimpleDateFormat dateFomrmat = new SimpleDateFormate("yyyy-MM-dd HH:mm:ss:SSS");
long registTime = mapper.get("数据库中的时间戳"); //“15111111111”
String time = dateFormate.formate(registTime)//2017-04-08 33:33:33
Date oldDate = dateFormate.parse(time); //Thu Mar 01 15:43:28 CST 2018
Calecdar cal = Calendar.getInstance();
cal.setTime(oldDate);
long 转换后的时间戳 = cal.getTimeInMillis();或者cal.getTime().getTime();

计算时间差

  1. 计算时间差
  2. 利用时间戳计算
    int days = (endtime-begintime)/(10006060*24);
    利用Calender类计算
  3. Date间相差天数
 public static int getDiffOfDate(Date oldDate,Date newDate){
	Calendar smallCalendar = Calendar.getInstance();
	smallCalendar.setTiem(oldDate);
	long beginTime = smallCalendar.TimeInMillis();
	Calendar bigCalendar = Calendar.getInstance();
	bigCalendar.setTime(newDate);
	long endTime = smallCalendar.TimeInMillis();
	int betweenDays = (int)((endTime-beginTime)/(1000*60*60*24));
	bigCalendar.add(Calendar.DAY_OF_MONTH,-betweenDays);
	bigCalendar.add(Calendar.DAY_OF_MONTH,-1);
	if(smallCalendar.get(Calendar.DAY_OF_MONTH)==bigCalendar.get(Calendar.DAY_OF_MONTH))) {
		betweenDays +=1;
	}
	return betweenDays;	
}

时间获取

1.获取本周的时间

public static List<Long> getCurrentWeekRange() throws ParseException(){
		List<Long> weekList = new Arraylist<>(2);
		Calendar cal = Calendar.getInstance();
		cal.setFirstDayOfWeek(Calendar.Mondy);
		cal.setTime
	}

生成时间戳

  1. System.currentTimeMillis(),它属于java.lang.System;
  2. Calendar.getInstance().getTimeInMillis(),它属于java.util.Calendar;
  3. new Date().getTime(),它属于java.util.Date;
  4. formate.parse().getTime()

通过上述比较,以运行100000为例,运行速度上currentTimeMillis>getTime>getTimeInMillis.
所以说在生成当前时间戳上,我建议大家采用currentTimeMillis方法,因为该方法运行速度最快

其他时间类

  1. Calender常用方法
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值