DateUtil

/**
 * @author 
 * @date 2017年10月17日-下午1:56:05
 */
package com.zte.claa.infiboss.utils;

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


public class DateUtil {
	// datetimeFormat
	public static final SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	
	// 防止实例化
	private DateUtil(){
		
	}
	
	/**
	 * 格式化日期.
	 * 
	 * @param Date 
	 * @return yyyy-MM-dd 格式的字符串
	 */
	public static String formatDate(Date d){
		return dateFormat.format(d);
	}
	
	/**
	 * 
	 * @param t
	 * @return yyyy-MM-dd HH:mm:ss 格式字符串.
	 */
	public static String formatTimestamp(Timestamp t){
		return datetimeFormat.format(t);
	}
	
	/**
	 * 
	 * @param tm
	 * @return
	 */
	public static String formatDateTime(long tm){
		return datetimeFormat.format(new Date(tm));
	}
	
	/**
	 * 解析日期时间字符串
	 * 
	 * @param dateStr
	 * @return
	 */
	public static Timestamp parseDatetime(String dateStr){
		try {
			Date d = datetimeFormat.parse(dateStr);
			return new Timestamp(d.getTime());
		} catch (ParseException e) {
			return null;
		}
	}
	
	/**
	 * 解析日期时间字符串
	 * 
	 * @param dateStr
	 * @return
	 */
	public static Timestamp parseDate(String dateStr){
		try {
			Date d = dateFormat.parse(dateStr);
			return new Timestamp(d.getTime());
		} catch (ParseException e) {
			return null;
		}
	}
	
	/**
	 * 解析日期时间字符串
	 * 
	 * @param timeStr  HH:mm:ss
	 * @return date
	 */
	public static Timestamp parseDateWithFixTime(Date d, String timeStr){
		try {
			String dStr = dateFormat.format(d);
			Date dt = datetimeFormat.parse(dStr+" "+timeStr);
			return new Timestamp(dt.getTime());
		} catch (ParseException e) {
			return null;
		}
	}
	
	/**
	 * 解析日期时间字符串
	 * 
	 * @param timeStr  HH:mm:ss
	 * @return date
	 */
	public static Timestamp parseCurDateWithFixTime(String timeStr){
		return DateUtil.parseDateWithFixTime(new Date(System.currentTimeMillis()), timeStr);
	}
	
	/**
	 * 返回日期是否在指定的日期范围内
	 * @param date
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static boolean isDateInRange(Date date, Timestamp startDate, Timestamp endDate){
		
		if (date.before(startDate) || date.after(endDate)){
			return false;
		}
		return true;
	}
	
	
	/**
	 * 获取当月的第一天
	 * @param date
	 * @return
	 */
	public static Date getFirstDayThisMonth(Date date){
		
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		
		return calendar.getTime();
	}
	
	public static void main(String[] args){		
		/*
		String str = "2017-10-17";
		Timestamp ts1 = DateUtil.parseDate(str);
		System.out.println(new Date(ts1.getTime()));
		str = "2017-10-17 01:00:00";
		Timestamp ts2 = DateUtil.parseDatetime(str);
		System.out.println(new Date(ts2.getTime()));
		System.out.println(DateUtil.parseCurDateWithFixTime("11:10:00"));
		*/		
		long ts = 1532584842 * 1000L;
		System.out.println(new Date(ts));
		if(true){
			return;
		}
		
		for (int i = 0; i < 50; i++) {
			UUID uuid = UUID.randomUUID();
			StringBuffer buf = new StringBuffer("U");
			buf.append(Math.abs(uuid.getLeastSignificantBits())).append(" -- ");
			buf.append(Long.toUnsignedString(uuid.getLeastSignificantBits()));
			// buf.append(Long.toUnsignedString(uuid.getMostSignificantBits(),16).toUpperCase());
			System.out.println(buf.toString());
		}
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZHOU_VIP

您的鼓励将是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值