Android Milliseconds Calculate millisecondsAfter(String date, String time, int milliseconds)

Here you can find the source of millisecondsAfter(String date, String time, int milliseconds)

Description

milliseconds After

Declaration

public static String[] millisecondsAfter(String date, String time,
            int milliseconds) 

Method Source Code

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

import java.util.Date;
import java.util.StringTokenizer;
import android.text.format.Time;

public class Main {
    public static String[] millisecondsAfter(String date, String time,
            int milliseconds) {
        Date d = dateTimeToDateObj(date, time);

        java.util.Calendar cal = java.util.Calendar.getInstance();

        cal.setTime(d);//from   w ww. ja v a  2 s.c  o m

        Time msAfter = new Time();

        msAfter.set(cal.getTimeInMillis() + milliseconds);

        String[] oha = { msAfter.format("%m-%d-%Y"),
                msAfter.format("%I:%M %p").toUpperCase() };

        return oha;
    }

    public static Date dateTimeToDateObj(String date, String time) {
        java.util.Calendar cal = java.util.Calendar.getInstance();

        int[] dateTokens;
        dateTokens = tokenizeDate(date);
        cal.set(java.util.Calendar.YEAR, dateTokens[2]);
        cal.set(java.util.Calendar.MONTH, dateTokens[0] - 1);
        cal.set(java.util.Calendar.DATE, dateTokens[1]);

        int[] timeTokens;
        timeTokens = tokenizeTime(time);
        if (timeTokens[0] == 12) {
            timeTokens[0] -= 12;
        }
        cal.set(java.util.Calendar.HOUR, timeTokens[0]);
        cal.set(java.util.Calendar.MINUTE, timeTokens[1]);
        cal.set(java.util.Calendar.SECOND, 0);
        cal.set(java.util.Calendar.MILLISECOND, 0);
        cal.set(java.util.Calendar.AM_PM, timeTokens[2]);

        return new Date(cal.getTimeInMillis());
    }

    public static int[] tokenizeDate(String date) {
        StringTokenizer st = new StringTokenizer(date, "-/");

        ArrayList<Integer> al = new ArrayList<Integer>();

        while (st.hasMoreTokens()) {
            al.add(Integer.parseInt(st.nextToken()));
        }

        int[] tokenList = new int[3];

        for (int i = 0; i < al.size(); i++) {
            tokenList[i] = al.get(i);
        }

        return tokenList;
    }

    public static int[] tokenizeTime(String time) {
        String newTime = time.replaceAll(":", " ").replaceAll("PM", "1")
                .replaceAll("AM", "0"); // 11:27 PM will become "11 17 1"

        StringTokenizer st = new StringTokenizer(newTime, " ");

        ArrayList<Integer> al = new ArrayList<Integer>();

        while (st.hasMoreTokens()) {
            al.add(Integer.parseInt(st.nextToken()));
        }

        int[] tokenList = new int[3];

        for (int i = 0; i < al.size(); i++) {
            tokenList[i] = al.get(i);
        }

        return tokenList;
    }
}

Related

  1. millisecondsAfterNearestFifteenMinutes( String date, String time, int milliseconds)
  2. formatDuration(final Resources res, final long millis)
  3. getMillisPerUnit(int unit)
  4. getUTCTimeMillis()
  5. getTimeZoneOffsetInMillis()