java Calendar与SimpleDateFormat

Calendar与SimpleDateFormat的异同


在开发中经常用到时间处理对于,一般来说有时间格式化处理,时间查询等。时间格式化一般使用SimpleDateFormat进行,但在日常中我们可能不能仅仅查询当前时间,可能查询之间某个时间端的(如昨天的0点到昨天12点)这个时候Calendar的优势就显现出来了,因为它能简单明了查询什么时间段的数据。

public class GetTime {
	private final static String NOWFORM = "yyyy-MM-dd HH:mm:ss";
	private final static String EXCELFORM = "yyyy-MM-dd_HH-mm-ss";
	private final static String STARTFORM = "yyyy-MM-dd 00:00:00";
	private final static String ENDFORM = "yyyy-MM-dd 23:59:59";
	private final static String OLDFORM = "yyyy-MM-dd";
	private final static String STRFORM = "yyyy年MM月dd日";
	private static SimpleDateFormat sdf;
	private static Calendar today;
	//创建一个Calendar对象
	public static Calendar getLogTime(){
		today = Calendar.getInstance();
		return  today;
	}
	//将当前时间按照NOWFORM进行格式化
	public static String getNowTime(){
		today = Calendar.getInstance();
		sdf = new SimpleDateFormat(NOWFORM);
		return  sdf.format(today.getTime());
	}
	//将当前时间按照STRFORM进行格式化
	public static String getStrNowTime(){
		today = Calendar.getInstance();
		sdf = new SimpleDateFormat(STRFORM);
		return  sdf.format(today.getTime());
	}
	//将当前时间按照yyyy-MM-dd 00:00:00进行格式化(格式化出来为当天0点)
	public static String getStartTime(){
		today = Calendar.getInstance();
		sdf= new SimpleDateFormat(STARTFORM);
		return  sdf.format(today.getTime());
	}
	//将当前时间按照yyyy-MM-dd 23:59:59进行格式化(格式化出来为当天23:59:59点)
	public static String getEndTime(){
		today = Calendar.getInstance();
		sdf= new SimpleDateFormat(ENDFORM);
		return  sdf.format(today.getTime());
	}
	//将当前时间按照yyyy-MM-dd_HH-mm-ss进行格式化
	public static String getExcelTime(){
		today = Calendar.getInstance();
		sdf= new SimpleDateFormat(EXCELFORM);
		return  sdf.format(today.getTime());
	}
	//oracle时间格式化  字符串转时间24小时制
	public static String getToDateFont(String dateTime){
		return "to_date('" + dateTime + "','YYYY-MM-DD HH24:MI:SS')";
	}
	//oracle时间格式化 当前时间yyyy-MM-dd 00:00:00按照YYYY-MM-DD HH24:MI:SS格式化进行格式 字符串转时间24小时制
	public static String getStartFont(){
		return "to_date('" + GetTime.getStartTime() + "','YYYY-MM-DD HH24:MI:SS')";
	}
	public static String getEndFont(){
		return "to_date('" + GetTime.getEndTime() + "','YYYY-MM-DD HH24:MI:SS')";
	}
	//按照毫秒数数据库中取出时间然后加上减去10天
	public static String getOldTiem(){
		today = Calendar.getInstance();
	    sdf = new SimpleDateFormat(OLDFORM);
	    long quot = today.getTime().getTime() - (long)10*1000*60*60*24;
		return sdf.format(quot);
	}
	public static String getStringTime(Date date){
		sdf = new SimpleDateFormat(NOWFORM);
		return sdf.format(date);
	}
	
	public static String getStringDate(String date){
		Integer time = Integer.parseInt(date);
		long days = time / (1000 * 60 * 60 * 24);  
	    long hours = (time % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);  
	    long minutes = (time % (1000 * 60 * 60)) / (1000 * 60);  
	    long seconds = (time % (1000 * 60)) / 1000;
		return days+"天"+hours+"时"+minutes+"分"+seconds+"秒";
	}
	
	public static Date strToDate(String str){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
}

查询前几天的时间num为天数

private Date addDay(Date nowTime,Integer num){
		Calendar instance = Calendar.getInstance();
		instance.setTime(nowTime);
		instance.add(Calendar.DATE, num);
		return instance.getTime();
	}
	Date addDay = addDay(nTime, 1);
	sTime = sdf.format(指定时间);
   eTime = sdf.format(addDay);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值