DateUtils

    /**
     * @param period
     *            分钟数值
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getMinuteTime(int period)
    {
        String rtn = "";
        long ticks = System.currentTimeMillis();
     //将时间戳划分为分钟取整,计算。 ticks
= ((ticks / (period * 60 * 1000)) - 1) * period * 60 * 1000; Date date = new Date(ticks); SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); rtn = fm.format(date); return rtn; }

啦啦啦

package com.xindatai.common.util;

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

public class DateUtils {

    public static final String DATE_FORMAT_BASE = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMAT_SNUMBER_DAY = "yyyyMMdd";

    /**
     * @param date
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getCurrMinuteTime()
    {
        String rtn = "";
        long ticks = System.currentTimeMillis();
        ticks = ((ticks / (1 * 60 * 1000))) * 1 * 60 * 1000;
        Date date = new Date(ticks);
        SimpleDateFormat fm = new SimpleDateFormat(DATE_FORMAT_BASE);
        rtn = fm.format(date);

        return rtn;
    }

    /**
     * 计算两个时间的时间差,单位为分钟
     * 
     * @param time1
     *            时间格式为:yyyy-MM-dd HH:mm:ss
     * @param time2
     *            时间格式为:yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static int getDiffMinutes(String time1, String time2)
    {
        int val = 0;
        try {
            Date date1 = org.apache.commons.lang3.time.DateUtils.parseDate(time1, DATE_FORMAT_BASE);
            Date date2 = org.apache.commons.lang3.time.DateUtils.parseDate(time2, DATE_FORMAT_BASE);
            Long temp = Math.abs(date1.getTime() / (1000 * 60) - date2.getTime() / (1000 * 60));
            val = temp.intValue();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return val;
    }

    /**
     * @param period
     *            分钟数值
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getMinuteTime(int period)
    {
        String rtn = "";
        long ticks = System.currentTimeMillis();
        ticks = ((ticks / (period * 60 * 1000)) - 1) * period * 60 * 1000;
        Date date = new Date(ticks);
        SimpleDateFormat fm = new SimpleDateFormat(DATE_FORMAT_BASE);
        rtn = fm.format(date);

        return rtn;
    }

    /**
     * @param dateStr
     * @return yyyy-MM-dd HH:mm
     */
    public static Date getDateTimeBystr(String dateStr)
    {
        if (dateStr == null || dateStr.length() == 0) {
            return null;
        }
        SimpleDateFormat fm = new SimpleDateFormat(DATE_FORMAT_BASE);
        try {
            return fm.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String getStringByFormat(Date date, String dateFormatSnumberDay) {
        if (date == null)
            return "";
        SimpleDateFormat fm = new SimpleDateFormat(dateFormatSnumberDay);
        return fm.format(date);
    }

    public static void main(String[] args) {

        System.out.println(getMinuteTime(5));
    }

}

啦啦啦

posted @ 2017-03-09 13:47  limeOracle  阅读(290)  评论(0编辑  收藏  举报