Here you can find the source of getTimeMargin(String dateTime)
public static long getTimeMargin(String dateTime)
//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(); } }