Java Long Number to Time toLong(String time)

Here you can find the source of toLong(String time)

Description

to Long

License

Apache License

Declaration

public static long toLong(String time) 

Method Source Code


//package com.java2s;
/*//w  w w . ja  va 2 s  .com
 * Copyright (C) 2016 Adam Huang <poisondog@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;

public class Main {
    public static long toLong(String time) {
        try {
            return completeToLong(time);
        } catch (ParseException e) {
        }
        try {
            return completeWithoutZoneToLong(time);
        } catch (ParseException e) {
        }
        try {
            return minSecondToLong(time);
        } catch (ParseException e) {
        }
        try {
            return minSecondWithoutZoneToLong(time);
        } catch (ParseException e) {
        }
        try {
            return minMinToLong(time);
        } catch (ParseException e) {
        }
        try {
            return minMinWithoutZoneToLong(time);
        } catch (ParseException e) {
        }
        try {
            return minHourToLong(time);
        } catch (ParseException e) {
        }
        try {
            return minHourWithoutZoneToLong(time);
        } catch (ParseException e) {
        }
        try {
            return minDayToLong(time);
        } catch (ParseException e) {
        }
        try {
            return minDayWithoutZoneToLong(time);
        } catch (ParseException e) {
        }
        throw new IllegalArgumentException("can't parse this time format.");
    }

    private static long completeToLong(String time) throws ParseException {
        return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").parse(time).getTime();
    }

    private static long completeWithoutZoneToLong(String time) throws ParseException {
        return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").parse(time).getTime();
    }

    private static long minSecondToLong(String time) throws ParseException {
        return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse(time).getTime();
    }

    private static long minSecondWithoutZoneToLong(String time) throws ParseException {
        return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(time).getTime();
    }

    private static long minMinToLong(String time) throws ParseException {
        return new SimpleDateFormat("yyyy-MM-dd'T'HH:mmZ").parse(time).getTime();
    }

    private static long minMinWithoutZoneToLong(String time) throws ParseException {
        return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm").parse(time).getTime();
    }

    private static long minHourToLong(String time) throws ParseException {
        return new SimpleDateFormat("yyyy-MM-dd'T'HHZ").parse(time).getTime();
    }

    private static long minHourWithoutZoneToLong(String time) throws ParseException {
        return new SimpleDateFormat("yyyy-MM-dd'T'HH").parse(time).getTime();
    }

    private static long minDayToLong(String time) throws ParseException {
        return new SimpleDateFormat("yyyy-MM-ddZ").parse(time).getTime();
    }

    private static long minDayWithoutZoneToLong(String time) throws ParseException {
        return new SimpleDateFormat("yyyy-MM-dd").parse(time).getTime();
    }
}

Related

  1. str2Long(String time)
  2. stringTime(long time)
  3. stringTolong(String time)
  4. time2Date(Long time)
  5. time2DATETIME(long time)
  6. toLongString(long ms)
  7. toTextTime(long time)
  8. transformDateTime(long t)