Java Milliseconds milliDate(String date)

Here you can find the source of milliDate(String date)

Description

Convert a YYYYMMDDHHMMSSTTT date to milliseconds

License

Open Source License

Declaration

public static long milliDate(String date) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.*;

public class Main {
    /**//w  ww. java2s .co m
     * Convert a YYYYMMDDHHMMSSTTT date to milliseconds
     */
    public static long milliDate(String date) {
        Calendar cal = new GregorianCalendar();
        try {
            cal.set(Integer.parseInt(date.substring(0, 4)), Integer.parseInt(date.substring(4, 6)),
                    Integer.parseInt(date.substring(6, 8)), Integer.parseInt(date.substring(8, 10)),
                    Integer.parseInt(date.substring(10, 12)), Integer.parseInt(date.substring(12, 14)));

            long milli = cal.getTimeInMillis() + Long.parseLong(date.substring(14));

            return milli;

        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * Convert a string to a int, and allow for an empty or null string
     */
    public static int parseInt(String s) {
        if (isEmpty(s))
            return 0;
        else
            return Integer.parseInt(s);
    }

    /**
     * Convert a string to a long, and allow for an empty or null string
     */
    public static long parseLong(String s) {
        if (isEmpty(s))
            return 0;
        else
            return Long.parseLong(s);
    }

    /**
     * test if the string passed is null or empty
     */
    public static final boolean isEmpty(String s) {
        return s == null || s.equals("");
    }

    /**
     * test if the StringBuffer passed is null or empty
     */
    public static final boolean isEmpty(StringBuffer sb) {
        return sb == null || sb.length() == 0;
    }

    /**
     * test if the byte [] passed is null or empty
     */
    public static final boolean isEmpty(byte[] b) {
        return b == null || b.length == 0;
    }
}

Related

  1. longToDate(long millis)
  2. marsTimeFromElapsedMillis(long elapsedMillis, int[] marsTime)
  3. megaCyclesToMilliseconds(long megaCycles)
  4. microsToMillis(long micros)
  5. microsToMillis(long sec, long usec)
  6. millisBetween(Date firstDate, Date lastDate)
  7. millisecond(final Date date)
  8. millisecondOf(Date date)
  9. milliseconds(int offset)