Java Time to String timeToDeparture(String startTime)

Here you can find the source of timeToDeparture(String startTime)

Description

Calculates minutes from now to a specific time in the future

License

Apache License

Parameter

Parameter Description
String startTime in format 2012-10-15T08:17:00

Return

minutes to departure

Declaration

public static String timeToDeparture(String startTime) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main {
    /**/*  ww w  .  j  av a 2 s .c  om*/
     *Calculates minutes from now to a specific time in the future
     * @param String startTime in format 2012-10-15T08:17:00
     * @return minutes to departure
     * */
    public static String timeToDeparture(String startTime) {
        int diffMinutes = -1;
        Calendar now = Calendar.getInstance();
        now.setTime(new Date());
        //now.setTimeZone(TimeZone.getTimeZone("Europe/Stockholm"));
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd'T'HH:mm:ss");
        Date date = null;
        try {
            date = dateFormat.parse(startTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        long millisDiff = cal.getTimeInMillis() - now.getTimeInMillis();
        if (millisDiff > 0) {
            diffMinutes = ((int) millisDiff / (60 * 1000));
        }
        return String.valueOf(diffMinutes);
    }
}

Related

  1. timeStr(Date _Date)
  2. timeString()
  3. timeString(double time)
  4. timeString(long tickNo, long offset)
  5. timeString2Date(String time)
  6. timeToFullString(long time)
  7. timeToFullString(long time, boolean longNames)
  8. timeToStr(int time)
  9. timeToStr(String time)