Java Time Print standardTime(Date date)

Here you can find the source of standardTime(Date date)

Description

standard Time

License

Apache License

Declaration

public static Date standardTime(Date date) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

public class Main {
    public static Date standardTime(Date date) {
        Calendar c = new GregorianCalendar();
        c.setTime(date);/*from ww w  .j a va2 s. co m*/
        int minute = c.get(Calendar.MINUTE);
        int gewei = minute % 15;
        int shiwei = minute / 15 * 15;
        if (gewei >= 7) {
            gewei = 15;
        } else {
            gewei = 0;
        }
        minute = gewei + shiwei;
        c.set(Calendar.MINUTE, minute);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);

        return c.getTime();
    }

    public static Date standardTime(Date date, int min) {
        Calendar c = new GregorianCalendar();
        c.setTime(date);
        int minute = c.get(Calendar.MINUTE);
        int gewei = minute % min;
        int shiwei = minute / min * min;
        if (gewei >= min / 2) {
            gewei = min;
        } else {
            gewei = 0;
        }
        minute = gewei + shiwei;
        c.set(Calendar.MINUTE, minute);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);

        return c.getTime();
    }

    public static Date standardTime() {
        return standardTime(new Date());

    }

    public static Date standardTime(long interval) {
        Calendar c = new GregorianCalendar();
        c.setTime(new Date());
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        while (Math.abs(c.getTimeInMillis() - new Date().getTime()) > interval / 2d) {
            c.setTimeInMillis(c.getTimeInMillis() + interval);
        }
        return c.getTime();
    }

    public static long getTime(String s) {
        return parseMysql(s).getTime();
    }

    public static Date parseMysql(String s) {
        try {
            DateFormat fmtTemp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return fmtTemp.parse(s);
        } catch (ParseException e) {
            return null;
        }
    }
}

Related

  1. printTimer(String blah)
  2. printTimes(String what, long... args)
  3. printTimestamp()
  4. printTimeStamp(final String msg)
  5. printTimeTakenMs(String msg, long start, long finish)