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

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

Description

milliseconds After Nearest Fifteen Minutes

Declaration

public static String[] millisecondsAfterNearestFifteenMinutes(
            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[] millisecondsAfterNearestFifteenMinutes(
            String date, String time, int milliseconds) {
        Date d = dateTimeToDateObj(date, time);

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

        cal.setTime(d);//from  w  w  w  .j a v a 2  s .  c  om

        Time msAfter = new Time();

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

        msAfter = nearestFifteenMinutes(msAfter);

        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 Time nearestFifteenMinutes(Time t) {
        Time nearestFifteen = t;

        if (t.minute < 8) {
            t.minute = 0;
        } else if (t.minute < 23) {
            t.minute = 15;
        } else if (t.minute < 38) {
            t.minute = 30;
        } else if (t.minute < 53) {
            t.minute = 45;
        } else {
            t.minute = 0;
            t.hour++;
        }

        return nearestFifteen;
    }

    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. millisecondsAfter(String date, String time, int milliseconds)
  2. formatDuration(final Resources res, final long millis)
  3. getMillisPerUnit(int unit)
  4. getUTCTimeMillis()