DateUtils.java 5.74 KB
package com.yoho.unions.utils;

import org.apache.commons.lang3.ArrayUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public final class DateUtils {

	public final static String DEFAULT_FOMARTPATTER="yyyy-MM-dd HH:mm:ss";

	public final static String DAY_FOMARTPATTER="yyyy-MM-dd";

	public final static String POINT_FOMARTPATTER="yyyy.MM.dd HH:mm";
	
	public final static String ACTIVITY_DAY_FOMARTPATTER="yyyy/MM/dd HH:mm";

	// 基础(通用)的FORMAT类型,
	public static final String NORMAL_FORMAT = "yyyy-MM-dd";


	/**
	 * 将数据库中的UNIX_Time(该时间是距离1970年的秒数,在转换过程中先要换算成毫秒)转换成UTC时间
	 * @param time
	 * @return
	 */
	public static String getDateString(int time)
	{
		SimpleDateFormat sdf=new SimpleDateFormat(DEFAULT_FOMARTPATTER);
		return sdf.format(new Date((long)time*1000));
	}
	
	/**
	 * 按照格式,取得当前时间
	 * @param str
	 * @return
	 */
	public static String getToday(String str) {
		return getDateFormatStr(new Date(), str);
	}
	
	/**
	 * 获取今天的日期时间(yyyy-MM-dd HH:mm:ss)
	 * @return
	 */
	public static String getcurrentDateTime() {
		return getToday(DEFAULT_FOMARTPATTER);
	}
	
	/**
	 * 
	 * @param time  获取数据库中的UNIX_Time(该时间是距离1970年的秒数,在转换过程中先要换算成毫秒)中的月份信息
	 * @return
	 */
	public static int getDateOfMonth(long time)
	{	
		Calendar c=Calendar.getInstance(TimeZone.getTimeZone("UTC"));
		c.setTime(new Date (time*1000));
		return c.get(Calendar.MONTH)+1;
	}
	
	/**
	 * @param time 获取数据库中的UNIX_Time(该时间是距离1970年的秒数,在转换过程中先要换算成毫秒)中的天信息 
	 * @return
	 */
	public static int getDateOfDay(long time)
	{	
		Calendar c=Calendar.getInstance(TimeZone.getTimeZone("UTC"));
		c.setTime(new Date (time*1000));
		return c.get(Calendar.DAY_OF_MONTH);
	}
	/**获得日期转换的秒
	 * @param  date
	 * @return
	 */
	public static int toSecond(Date date) {
		long longTime = date.getTime();
        return (int)(longTime /1000);
	}

	/**
	 * 获取字符串格式的时间毫秒数
	 * @param time
	 * @return
	 */
	public static long getDateOfSenconds(String time)
	{
		long timeLong = 0L;
		SimpleDateFormat sdf=new SimpleDateFormat(DEFAULT_FOMARTPATTER);
		Date date;
		try {
			date = sdf.parse(time);
			timeLong = date.getTime()/1000;
		} catch (ParseException e) {
			
		}
		return timeLong;
	}
	
	/**
	 * 将通过getTime获得的long型的时间,转换成format类型的时间,如"2015-08-09"
	 * 
	 * @param time long型的时间戳
	 * @param format 需要转化的类型 “yyyy-MM-dd”
	 * @return String "2015-08-09"
	 */
	public static String long2DateStr(long time, String format) {
		Date date = new Date(time);
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		String dateStr = sdf.format(date);
		return dateStr;
	}

	/**
	 * 根据Date类型的时间, 将时间转换成字符串类型的时间. format: 时间模版
	 *
	 * @param date Date 时间
	 * @param format 模板,例: "yyyy-MM-dd HH:mm:ss"
	 * @return String 字符串类型的时间
	 *
	 * @author create by dengxinfei on 2016-12-27
	 */
	public static String getDateFormatStr(Date date, String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		String dateStr = sdf.format(date);
		return dateStr;
	}
	
	/**
	 * 获取当前时间的距离 1970-01-01的秒数
	 * @return
	 */
	public static int getCurrentTimeSecond(){
		Date date = new Date();
		int second = (int)(date.getTime() / 1000);
		return second;
	}

	/**
	 * 获取当天零点时间
	 * @return
	 */
	public static int getTodayZero() {
        Calendar today = Calendar.getInstance();  
        today.set(Calendar.HOUR_OF_DAY, 0);  
        today.set(Calendar.MINUTE, 0);  
        today.set(Calendar.SECOND, 0);  
        today.set(Calendar.MILLISECOND, 0);  
        return (int)(today.getTime().getTime() / 1000);
	}

	/**
	 * 将通过getTime获得的long型的时间,转换成format类型的时间,如"2015-08-09"
	 *
	 * @param time
	 *            int型的时间戳
	 * @param format
	 *            需要转化的类型 “yyyy-MM-dd”
	 * @return String "2015-08-09"
	 */
	public static String int2DateStr(int time, String... format) {
		if (time == 0) {
			return "";
		}
		Date date = new Date((long) time * 1000);
		String f = "";
		if (ArrayUtils.isEmpty(format)) {
			f = NORMAL_FORMAT;
		} else {
			f = (format == null) ? NORMAL_FORMAT : format[0];
		}
		SimpleDateFormat sdf = new SimpleDateFormat(f);
		String dateStr = sdf.format(date);
		return dateStr;
	}
	public static void main(String[] args) {
		System.out.print(getTimesmorning("2017/03/06"));
	}
	
	/**
	 * 将数据库中的UNIX_Time(该时间是距离1970年的秒数,在转换过程中先要换算成毫秒)转换成UTC时间
	 * @param time
	 * @return
	 */
	public static String getActivityDateString(int time)
	{
		SimpleDateFormat sdf=new SimpleDateFormat(ACTIVITY_DAY_FOMARTPATTER);
		return sdf.format(new Date((long)time*1000));
	}

	/**
	 * 获得某天0点时间戳
	 * @return
	 */
	public static int getTimesmorning(String dateStr){
		Date date = new Date(dateStr);
		return toSecond(date);
	}

	/**
	 * 获取前一天
	 * @param specifiedDay
	 * @param format
	 * @return
	 */
	public static String beforeDay(String specifiedDay,String format){
		Calendar c = Calendar.getInstance();
		Date date = null;
		try{
			date = new SimpleDateFormat(format).parse(specifiedDay);
		}catch (Exception e) {
			e.printStackTrace();
		}
		c.setTime(date);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day-1);
		String dayBefore = new SimpleDateFormat(format).format(c.getTime());
		return dayBefore;
	}


}