Java Date to Time getTimeMargin(String dateTime)

Here you can find the source of getTimeMargin(String dateTime)

Description

get Time Margin

License

Open Source License

Declaration

public static long getTimeMargin(String dateTime) 

Method Source Code


//package com.java2s;
import java.util.Calendar;

public class Main {

    public static long getTimeMargin(String dateTime) {
        int index = dateTime.indexOf(" ");
        String date = dateTime.substring(0, index);
        String time = dateTime.substring(index + 1);

        int dateSlash1 = date.indexOf("-");
        int dateSlash2 = date.lastIndexOf("-");

        if (dateSlash1 <= 0 || dateSlash1 == dateSlash2)
            return -1;

        int timeColon1 = time.indexOf(":");
        int timeColon2 = time.lastIndexOf(":");

        if (timeColon1 <= 0 || timeColon1 == timeColon2)
            return -1;

        Calendar calendar = Calendar.getInstance();

        try {//  w ww.  j a  v  a  2s  . com
            int year = Integer.parseInt(date.substring(0, dateSlash1));
            int month = Integer.parseInt(date.substring(dateSlash1 + 1, dateSlash2));
            int day = Integer.parseInt(date.substring(dateSlash2 + 1));

            int hour = Integer.parseInt(time.substring(0, timeColon1));
            int minute = Integer.parseInt(time.substring(timeColon1 + 1, timeColon2));
            int second = Integer.parseInt(time.substring(timeColon2 + 1));

            calendar.set(year, month - 1, day, hour, minute, second);
        } catch (Exception e) {
            return -1;
        }

        return System.currentTimeMillis() - calendar.getTimeInMillis();
    }
}

Related

  1. getTimeInSeconds(Date date)
  2. getTimeInterval(Date fromDate)
  3. getTimeIntervalMins(Date firstDate, Date lastDate)
  4. getTimeLocalWithoutDst(java.util.Date d)
  5. getTimeLocalWithoutDst(java.util.Date d)
  6. getTimeMax(Date date)
  7. getTimeOfDate(Date date)
  8. getTimeOnly(Date date)
  9. getTimeOnly(Date date)