All Downloads are FREE. Search and download functionalities are using the official Maven repository.

csip.utils.Dates Maven / Gradle / Ivy

Go to download

The Cloud Services Integration Platform is a SoA implementation to offer a Model-as-a-Service framework, Application Programming Interface, deployment infrastructure, and service implementations for environmental modeling.

There is a newer version: 2.6.30
Show newest version
/*
 * $Id: Dates.java c4b9d8c126c7 2020-07-28 [email protected] $
 *
 * This file is part of the Cloud Services Integration Platform (CSIP),
 * a Model-as-a-Service framework, API and application suite.
 *
 * 2012-2019, Olaf David and others, OMSLab, Colorado State University.
 *
 * OMSLab licenses this file to you under the MIT license.
 * See the LICENSE file in the project root for more information.
 */
package csip.utils;

import csip.Config;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.format.DateTimeParseException;
import java.util.*;

/**
 * All kind of stuff
 *
 * @author od
 */
public class Dates {

    static final long NUM_100NS_INTERVALS_SINCE_UUID_EPOCH = 0x01b21dd213814000L;


    public static long diffInMillis(Date start, Date end) {
        return end.getTime() - start.getTime();
    }


    public static DateFormat newISOFormat() {
//        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }


    public static Date parse(String iso) throws ParseException {
        return newISOFormat().parse(iso);
    }


    public static long getDurationSec(String key, long def) {
        String dur = Config.getString(key, Long.toString(def));
        try {
            return Duration.parse(dur).getSeconds();
        } catch (DateTimeParseException E) {
            return Config.getLong(key, def);
        }
    }


    public static long getDurationSec(String key) {
        String dur = Config.getString(key);
        try {
            return Duration.parse(dur).getSeconds();
        } catch (DateTimeParseException E) {
            return Config.getLong(key);
        }
    }


    public static String duration(Date start, Date end) {
        long millis = diffInMillis(start, end);
        return duration(millis);
    }


    public static String duration(long millis) {
        int h = (int) ((millis / 1000) / 3600);
        int m = (int) (((millis / 1000) / 60) % 60);
        int s = (int) ((millis / 1000) % 60);
        return String.format("%1$02d:%2$02d:%3$02d", h, m, s);
    }
    
    public static void main(String[] args) {
      for (int i = 0; i < 1000000; i+=1000 ) {
      System.out.print(duration(i));
      System.out.print(" ");
      }
    
    }


    public static DateFormat newISOFormat(String tz) {
        DateFormat df = newISOFormat();
        df.setTimeZone(TimeZone.getTimeZone(tz));
        return df;
    }


    /**
     * Get the current date/time in ISO.
     *
     * @return date/time in ISO format
     */
    public static String nowISO() {
        return newISOFormat().format(new Date());
    }


    public static String nowISO(String tz) {
        DateFormat f = newISOFormat(tz);
        return f.format(new Date());
    }


    /**
     * get the future date for 'now' + $sec seconds
     *
     * @param sec the seconds
     * @return future date
     */
    public static Date futureDate(long sec) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(new Date());
        cal.add(Calendar.SECOND, (int) sec);
        return cal.getTime();
    }


    public static Date futureDate(Date from, long sec) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(from);
        cal.add(Calendar.SECOND, (int) sec);
        return cal.getTime();
    }


    /**
     * Get the long time for the uuid.
     *
     * @param uuid the uuid
     * @return the long time for the input uuid
     */
    public static long getTimeFromUUID(UUID uuid) {
        return (uuid.timestamp() - NUM_100NS_INTERVALS_SINCE_UUID_EPOCH) / 10000;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy