Android Alarm Get getNextAlarmTimeSpan(String time)

Here you can find the source of getNextAlarmTimeSpan(String time)

Description

get Next Alarm Time Span

Declaration

public static long getNextAlarmTimeSpan(String time) 

Method Source Code

//package com.java2s;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import android.annotation.SuppressLint;

public class Main {
    public static Calendar calendar = Calendar.getInstance();
    public static String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

    public static long getNextAlarmTimeSpan(String time) {
        calendar.clear();//from  w  ww  . j av  a  2s .  co  m
        calendar.setTime(parseDate(time));
        return (calendar.getTimeInMillis() - System.currentTimeMillis()) / 1000; // ???????,??
    }

    public static Date parseDate(String date) {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(
                    DEFAULT_DATE_FORMAT);
            return dateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new Date();
    }

    @SuppressLint("SimpleDateFormat")
    public static long getTimeInMillis(String date) {
        Date currentDate;
        try {
            String format = "HH:mm";
            if (date.contains("-")) {
                format = "yyyy-MM-dd HH:mm:ss";
            }
            SimpleDateFormat dateForamt = new SimpleDateFormat(format);
            currentDate = dateForamt.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();

            currentDate = new Date();
        }
        calendar.setTime(currentDate);
        return calendar.getTimeInMillis();
    }
}

Related

  1. getAlarmManager(Context ctx)
  2. getAlarmManager(Context context)
  3. getDefaultAlarmUri()
  4. getDefaultAlarmUri()