Java Milliseconds stripSubMillis(String iso8601string)

Here you can find the source of stripSubMillis(String iso8601string)

Description

strip Sub Millis

License

Open Source License

Declaration

public static String stripSubMillis(String iso8601string) 

Method Source Code

//package com.java2s;
//  License: https://servicestack.net/bsd-license.txt

public class Main {
    public final static int isoDateWithSubMsMin = "YYYY-MM-DDT00:00:00.0000+00:00".length();
    public final static int isoDateWithSubMsMax = "YYYY-MM-DDT00:00:00.0000000+00:00".length();

    public static String stripSubMillis(String iso8601string) {
        if (iso8601string.length() < isoDateWithSubMsMin || iso8601string.length() > isoDateWithSubMsMax)
            return iso8601string;

        String[] parts = splitOnFirst(iso8601string, '.');

        String suffix = parts[1].substring(parts[1].length() - 6); //+00:00
        String ms = parts[1].substring(0, 3);

        return parts[0] + "." + ms + suffix;
    }/*  ww w  . ja v a 2 s .  c  o m*/

    public static String[] splitOnFirst(String strVal, char needle) {
        if (strVal == null)
            return new String[0];
        int pos = strVal.indexOf(needle);
        return pos == -1 ? new String[] { strVal }
                : new String[] { strVal.substring(0, pos), strVal.substring(pos + 1) };
    }

    public static String[] splitOnFirst(String strVal, String needle) {
        if (strVal == null)
            return new String[0];
        int pos = strVal.indexOf(needle);
        return pos == -1 ? new String[] { strVal }
                : new String[] { strVal.substring(0, pos), strVal.substring(pos + needle.length()) };
    }
}

Related

  1. setGlobalDebugMillis(int millis)
  2. setMilliseconds(Date date, int amount)
  3. setMilliSecondsFromSeconds(int seconds)
  4. setThreadSleepTimeInMillis(long inThreadSleepTimeInMillis)
  5. stripMillisFromJdbcTimestampString(String timestamp)
  6. strMillis(final long ns)
  7. strMillisOLD(long dlt)
  8. ticksToMilliseconds(long ticks)
  9. ticksWithMillisecondsSetTo(long ticks, int milliseconds)