get Month Time Millis Offset - Android java.util

Android examples for java.util:Month

Description

get Month Time Millis Offset

Demo Code


import java.util.Calendar;
import java.util.Date;

public class Main {

  public static long getMonthTimeMillisOffset(String year, String month,String day, int offset) {
    try {//from ww w  .  j  a va 2  s.  c  om
      if (day == null || day == "") {
        day = "01";
      }
      int y = Integer.valueOf(year);
      int m = Integer.valueOf(month) - 1;
      int d = Integer.valueOf(day);
      Calendar calendar = Calendar.getInstance();
      calendar.set(y, m, d);
      calendar.add(Calendar.MONTH, offset);
      return calendar.getTimeInMillis();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return -1;
  }

  @Deprecated
  public static long getMonthTimeMillisOffset(String date, int offset) {
    try {
      Date curDay = new Date(date);
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(curDay);
      int y = calendar.YEAR;
      int m = calendar.MONTH - 1;
      int d = calendar.DATE;
      calendar = Calendar.getInstance();
      calendar.set(y, m, d);
      calendar.add(Calendar.MONTH, offset);
      return calendar.getTimeInMillis();
    } catch (Exception e) {
      // System.out.println("-->" + e.getMessage());
      e.printStackTrace();
    }
    return -1;
  }

  public static long getMonthTimeMillisOffset(long timeMillis, int offset) {
    try {
      Calendar calendar = Calendar.getInstance();
      calendar.setTimeInMillis(timeMillis);
      int y = calendar.YEAR;
      int m = calendar.MONTH - 1;
      int d = calendar.DATE;
      calendar = Calendar.getInstance();
      calendar.set(y, m, d);
      calendar.add(Calendar.MONTH, offset);
      return calendar.getTimeInMillis();
    } catch (Exception e) {
      // System.out.println("-->" + e.getMessage());
      e.printStackTrace();
    }
    return -1;
  }

}

Related Tutorials