Java Code Examples for java.util.Calendar.setTimeInMillis()

The following are Jave code examples for showing how to use setTimeInMillis() of the java.util.Calendar class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: Botanist   File: DatabaseManager.java   View Source Code Vote up 6 votes
/**
 * Set when to remind about birthdays
 * @param context - the current app context
 * @param plant - the plant whose birthday is reminded of
 * @param id - the id of the plant
 */
public void setBirthdayReminder(Context context, Plant plant, int id) {
    Intent intent = new Intent(context, BirthdayReceiver.class);
    intent.putExtra("name", plant.getName());
    intent.putExtra("species", plant.getSpecies());
    intent.putExtra("birthday", plant.getBirthday());
    intent.putExtra("id", id);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id, intent, 0);
    Calendar now = Calendar.getInstance();
    now.setTimeInMillis(System.currentTimeMillis());
    Calendar birthday = Calendar.getInstance();
    birthday.setTimeInMillis(plant.getBirthday());
    birthday.set(Calendar.YEAR, now.get((Calendar.YEAR)));
    if (birthday.getTimeInMillis() < now.getTimeInMillis()) {
        birthday.set(Calendar.YEAR, now.get((Calendar.YEAR)) + 1);
    }
    AlarmManager am = (AlarmManager)context.getSystemService(ALARM_SERVICE);
    if (am != null) {
        am.set(AlarmManager.RTC_WAKEUP, birthday.getTimeInMillis(), pendingIntent);
    }
}
 
Example 2
Project: PlusGram   File: LocaleController.java   View Source Code Vote up 6 votes
public static String formatDateChat(long date) {
    try {
        Calendar rightNow = Calendar.getInstance();
        int year = rightNow.get(Calendar.YEAR);

        rightNow.setTimeInMillis(date * 1000);
        int dateYear = rightNow.get(Calendar.YEAR);

        if (year == dateYear) {
            return getInstance().chatDate.format(date * 1000);
        }
        return getInstance().chatFullDate.format(date * 1000);
    } catch (Exception e) {
        FileLog.e("tmessages", e);
    }
    return "LOC_ERR: formatDateChat";
}
 
Example 3
Project: aftercare-app-android   File: DCLocalNotificationsManager.java   View Source Code Vote up 6 votes
/**
 * Returns time in milliseconds for +1 day if the hour & minute has already passed that day
 * @param hourOfDay
 * @param minute
 * @return
 */
private long getRepeatingNotificationTime(int hourOfDay, int minute) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());

    if (calendar.get(Calendar.HOUR_OF_DAY) > hourOfDay) {
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
    }
    else if (calendar.get(Calendar.HOUR_OF_DAY) == hourOfDay) {
        if (calendar.get(Calendar.MINUTE) >= minute) {
            calendar.add(Calendar.DAY_OF_YEAR, 1);
            calendar.set(Calendar.MINUTE, minute);
        }
    } else {
        calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
        calendar.set(Calendar.MINUTE, minute);
    }

    return calendar.getTimeInMillis();
}
 
Example 4
Project: Calendouer   File: ClockWidgetProvider.java   View Source Code Vote up 6 votes
@Override
public void onEnabled(Context context) {
    super.onEnabled(context);
    AlarmManager alarmMgr = (AlarmManager) context.getSystemService(
            Context.ALARM_SERVICE);
    long intervalClock = 60000;
    long intervalWeather = 7200000;

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());

    alarmMgr.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),
            intervalClock, createClockTickIntent(context));

    alarmMgr.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),
            intervalWeather, createWeatherUpdateIntent(context));
}
 
Example 5
Project: SmartChart   File: CrashHandler.java   View Source Code Vote up 5 votes
private String toDateString(long timeMilli) {
    Calendar calc = Calendar.getInstance();
    calc.setTimeInMillis(timeMilli);
    return String.format(Locale.CHINESE, "%04d.%02d.%02d %02d:%02d:%02d:%03d",
            calc.get(Calendar.YEAR), calc.get(Calendar.MONTH) + 1, calc.get(Calendar.DAY_OF_MONTH),
            calc.get(Calendar.HOUR_OF_DAY), calc.get(Calendar.MINUTE), calc.get(Calendar.SECOND), calc.get(Calendar.MILLISECOND));
}
 
Example 6
Project: ChronoBike   File: Time_ms.java   View Source Code Vote up 5 votes
public static String formatYYYYMMDDHHMMSS_ms(long l)
{
	Calendar cal = new GregorianCalendar();
	cal.setTimeInMillis(l);
	
	Date date = cal.getTime();
	SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
	String cs = formatter.format(date) ;
	return cs ;
}
 
Example 7
Project: AutoOff   File: NotificationService.java   View Source Code Vote up 5 votes
@Override
public int onStartCommand(final Intent intent, int flags, int startId) {

    settingsprefs = getSharedPreferences("settings", 0);

    now = System.currentTimeMillis();

    if (intent != null) {
        if (intent.getExtras() != null) {
            mode = (String) intent.getExtras().get("mode");
            additionalShakeDelay = intent.getBooleanExtra("additionalShakeDelay", false);
        }
    }

    // We want this service to continue running until it is explicitly
    // stopped, so return sticky.
    nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    long endTime = 0;
    switch (mode) {
        case "boot":
            endTime = settingsprefs.getLong("bootShutdownTime", 0);
            break;
        case "minute":
            endTime = settingsprefs.getLong("minuteShutdownTime", 0);
            break;
        case "time":
            endTime = settingsprefs.getLong("timeShutdownTime", 0);
            break;
    }
    lastendTime = endTime;
    duration = endTime - now;
    Calendar calll = Calendar.getInstance();
    calll.setTimeInMillis(endTime);
    createCountDownTimer();
    return START_STICKY;
}
 
Example 8
Project: ConnectU   File: UpdaterHelper.java   View Source Code Vote up 5 votes
private static boolean isTheSameYear(long value) {
    Calendar current = Calendar.getInstance();
    Calendar old = Calendar.getInstance();
    old.clear();
    old.setTimeInMillis(value);
    int cyear = current.get(Calendar.YEAR);
    int oyear = old.get(Calendar.YEAR);
    return cyear == oyear;
}
 
Example 9
Project: newwebrtc   File: ChatAdapter.java   View Source Code Vote up 5 votes
/**
 * Format the long System.currentTimeMillis() to a better looking timestamp. Uses a calendar
 *   object to format with the user's current time zone.
 * @param timeStamp
 * @return
 */
public static String formatTimeStamp(long timeStamp){
    // Create a DateFormatter object for displaying date in specified format.
    SimpleDateFormat formatter = new SimpleDateFormat("h:mm.ss a");

    // Create a calendar object that will convert the date and time value in milliseconds to date.
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(timeStamp);
    return formatter.format(calendar.getTime());
}
 
Example 10
Project: athena   File: TopologyCommand.java   View Source Code Vote up 5 votes
/**
 * Converts millis to a formatted Date String.
 *
 * @param millis Duration in millis to convert to a string
 *
 * @return Formatted string: yyyy-MM-dd HH:mm:ss.
 */
private static String formatCreationTime(long millis) {
    final DateFormat dateFormatter =
            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(millis);
    return (dateFormatter.format(calendar.getTime()));
}
 
Example 11
Project: jmt   File: GraphArrivalPanel.java   View Source Code Vote up 5 votes
/**
 * Is the method invoked to calculate the number of minutes in the log (Variable)
 */
private void ArrNumMinute() {
	ArrMinute = new Vector<double[]>();
	double i[];
	Calendar cal = Calendar.getInstance();
	cal.setTime(new Date((long) v.getValue(0)));
	Calendar cal2 = Calendar.getInstance();
	int y = 0;
	boolean k = false;
	int z = 0;
	cal2.setTimeInMillis((long) v.getValue(z));
	while (z < v.Size()) {
		if (cal.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal.get(Calendar.MONTH) == cal2.get(Calendar.MONTH)
				&& cal.get(Calendar.DATE) == cal2.get(Calendar.DATE) && cal.get(Calendar.HOUR_OF_DAY) == cal2.get(Calendar.HOUR_OF_DAY)
				&& cal.get(Calendar.MINUTE) == cal2.get(Calendar.MINUTE)) {
			y++;
		} else {
			k = false;
			i = new double[2];
			i[0] = cal.getTimeInMillis();
			i[1] = y;
			ArrMinute.addElement(i);
			if (cal.get(Calendar.MINUTE) == 59 && cal.get(Calendar.HOUR_OF_DAY) != 23) {
				cal.roll(Calendar.HOUR_OF_DAY, true);
			}
			if (cal.get(Calendar.MINUTE) == 59 && cal.get(Calendar.HOUR_OF_DAY) == 23) {
				cal.roll(Calendar.HOUR_OF_DAY, true);
				cal.roll(Calendar.DAY_OF_YEAR, true);
			}
			cal.roll(Calendar.MINUTE, true);
			if (cal.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal.get(Calendar.MONTH) == cal2.get(Calendar.MONTH)
					&& cal.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR)
					&& cal.get(Calendar.HOUR_OF_DAY) == cal2.get(Calendar.HOUR_OF_DAY) && cal.get(Calendar.MINUTE) == cal2.get(Calendar.MINUTE)) {
				y = 1;
			} else {
				y = 0;
				k = true;
			}
		}
		if (k != true) {
			z++;
			if (z < v.Size()) {
				cal2.setTimeInMillis((long) v.getValue(z));
			}
		}
	}
	i = new double[2];
	i[0] = cal.getTimeInMillis();
	i[1] = y;
	ArrMinute.addElement(i);
}
 
Example 12
Project: reading-and-annotate-rocketmq-3.4.6   File: UtilAll.java   View Source Code Vote up 5 votes
public static String timeMillisToHumanString(final long t) {
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(t);
    return String.format("%04d%02d%02d%02d%02d%02d%03d", cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1,
        cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND),
        cal.get(Calendar.MILLISECOND));
}
 
Example 13
Project: directory-ldap-api   File: GeneralizedTimeTest.java   View Source Code Vote up 5 votes
/**
 * Tests a complete round trip.
 */
@Test
public void testRoundTrip() throws ParseException
{
    Calendar calendar = new GregorianCalendar( GMT, Locale.ROOT );
    calendar.setTimeInMillis( 123456789000L ); // default format is without millis

    // create form calendar
    GeneralizedTime generalizedTime1 = new GeneralizedTime( calendar );

    // get the string value
    String gt1 = generalizedTime1.toGeneralizedTime();
    Calendar calendar1 = generalizedTime1.getCalendar();

    // create from string value
    GeneralizedTime generalizedTime2 = new GeneralizedTime( gt1 );

    // get the calendar value 
    Calendar calendar2 = generalizedTime2.getCalendar();
    String gt2 = generalizedTime2.toGeneralizedTime();

    // assert that all are equal
    assertEquals( calendar, calendar1 );
    assertEquals( calendar, calendar2 );
    assertEquals( calendar1, calendar2 );
    assertEquals( gt1, gt2 );
    assertTrue( calendar.isLenient() );
    assertTrue( calendar1.isLenient() );
    assertTrue( calendar2.isLenient() );
}
 
Example 14
Project: react-native-udesk   File: UdeskUtil.java   View Source Code Vote up 5 votes
public static String formatLongTypeTimeToString(Context context, long time) {
    long OFFSET_DAY = 3600 * 24;
    String timeYes = context.getString(R.string.udesk_im_time_format_yday);
    String timeQt = context.getString(R.string.udesk_im_time_format_dby);
    String timeDate = "yyyy/MM/dd";
    Calendar calendar = Calendar.getInstance();
    StringBuilder build = new StringBuilder();
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");

    // 解析需要转化时间
    calendar.setTimeInMillis(time);
    int year = calendar.get(Calendar.YEAR);
    int day = calendar.get(Calendar.DAY_OF_YEAR);

    // 拼接 转化结果
    build.append(" ").append(sdf.format(calendar.getTime()));// 先添加

    // 先解析当前时间。取出当前年,日 等信息
    calendar.setTimeInMillis(System.currentTimeMillis());
    int nowYear = calendar.get(Calendar.YEAR);
    int nowDay = calendar.get(Calendar.DAY_OF_YEAR);

    if (year != nowYear) {// 不是一年内
        calendar.set(Calendar.HOUR_OF_DAY, 0); // 凌晨1点
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        if ((calendar.getTimeInMillis() - time) <= OFFSET_DAY) {// 昨天
            return timeYes;
        } else if ((calendar.getTimeInMillis() - time) <= (OFFSET_DAY << 2)) {// 前天
            // 。这里不用判断是否大于OFFSET_DAY
            return timeQt;
        } else {
            sdf.applyLocalizedPattern(timeDate);
            return sdf.format(time);
        }

    } else if (day == nowDay) {// 这里是一年内的当天
        // 当天的话 就不用管了
    } else {// 一年内
        int dayOffset = (nowDay - day);// nowDay要大一些
        if (dayOffset == 0) {
            // 同一天不用 添加日期判断
        } else if (dayOffset == 1) {// 1表示差一天,即昨天
            return timeYes;
        } else if (dayOffset == 2) {// 1表示差两天,即前天
            return timeQt;
        } else {
            timeDate = "MM月dd日";
            sdf.applyLocalizedPattern(timeDate);
            return sdf.format(time);
        }
    }

    return build.toString();
}
 
Example 15
Project: GitHub   File: Kits.java   View Source Code Vote up 5 votes
/**
 * 获取当月第一天的时间(毫秒值)
 *
 * @param mills
 * @return
 */
public static long getFirstOfMonth(long mills) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(mills);
    calendar.set(Calendar.DAY_OF_MONTH, 1);

    return calendar.getTimeInMillis();
}
 
Example 16
Project: AssistantBySDK   File: TimeUtils.java   View Source Code Vote up 4 votes
public String getTimeBidUpMilliSecond(long bidUp) {
    Calendar cl = Calendar.getInstance();
    cl.setTimeInMillis(System.currentTimeMillis() + bidUp);

    return cl.get(Calendar.HOUR_OF_DAY) + ":" + cl.get(Calendar.MINUTE);
}
 
Example 17
Project: OTAUpdates   File: Utils.java   View Source Code Vote up 4 votes
public static String getDate(long time) {
    Calendar cal = Calendar.getInstance(Locale.getDefault());
    cal.setTimeInMillis(time);
    return DateFormat.format("dd-MM-yyyy", cal).toString();
}
 
Example 18
Project: RNLearn_Project1   File: DatePickerDialogFragment.java   View Source Code Vote up 4 votes
static Dialog createDialog(
    Bundle args, Context activityContext, @Nullable OnDateSetListener onDateSetListener) {
  final Calendar c = Calendar.getInstance();
  if (args != null && args.containsKey(DatePickerDialogModule.ARG_DATE)) {
    c.setTimeInMillis(args.getLong(DatePickerDialogModule.ARG_DATE));
  }
  final int year = c.get(Calendar.YEAR);
  final int month = c.get(Calendar.MONTH);
  final int day = c.get(Calendar.DAY_OF_MONTH);

  DatePickerMode mode = DatePickerMode.DEFAULT;
  if (args != null && args.getString(DatePickerDialogModule.ARG_MODE, null) != null) {
    mode = DatePickerMode.valueOf(args.getString(DatePickerDialogModule.ARG_MODE).toUpperCase(Locale.US));
  }

  DatePickerDialog dialog = null;

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    switch (mode) {
      case CALENDAR:
        dialog = new DismissableDatePickerDialog(activityContext,
          activityContext.getResources().getIdentifier("CalendarDatePickerDialog", "style", activityContext.getPackageName()),
          onDateSetListener, year, month, day);
        break;
      case SPINNER:
        dialog = new DismissableDatePickerDialog(activityContext,
          activityContext.getResources().getIdentifier("SpinnerDatePickerDialog", "style", activityContext.getPackageName()),
          onDateSetListener, year, month, day);
        break;
      case DEFAULT:
        dialog = new DismissableDatePickerDialog(activityContext, onDateSetListener, year, month, day);
        break;
    }
  } else {
    dialog = new DismissableDatePickerDialog(activityContext, onDateSetListener, year, month, day);

    switch (mode) {
      case CALENDAR:
        dialog.getDatePicker().setCalendarViewShown(true);
        dialog.getDatePicker().setSpinnersShown(false);
        break;
      case SPINNER:
        dialog.getDatePicker().setCalendarViewShown(false);
        break;
    }
  }

  final DatePicker datePicker = dialog.getDatePicker();

  if (args != null && args.containsKey(DatePickerDialogModule.ARG_MINDATE)) {
    // Set minDate to the beginning of the day. We need this because of clowniness in datepicker
    // that causes it to throw an exception if minDate is greater than the internal timestamp
    // that it generates from the y/m/d passed in the constructor.
    c.setTimeInMillis(args.getLong(DatePickerDialogModule.ARG_MINDATE));
    c.set(Calendar.HOUR_OF_DAY, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);
    datePicker.setMinDate(c.getTimeInMillis());
  } else {
    // This is to work around a bug in DatePickerDialog where it doesn't display a title showing
    // the date under certain conditions.
    datePicker.setMinDate(DEFAULT_MIN_DATE);
  }
  if (args != null && args.containsKey(DatePickerDialogModule.ARG_MAXDATE)) {
    // Set maxDate to the end of the day, same reason as for minDate.
    c.setTimeInMillis(args.getLong(DatePickerDialogModule.ARG_MAXDATE));
    c.set(Calendar.HOUR_OF_DAY, 23);
    c.set(Calendar.MINUTE, 59);
    c.set(Calendar.SECOND, 59);
    c.set(Calendar.MILLISECOND, 999);
    datePicker.setMaxDate(c.getTimeInMillis());
  }

  return dialog;
}
 
Example 19
Project: fuck_zookeeper   File: GenerateLoad.java   View Source Code Vote up 4 votes
public void run() {
    try {
        currentInterval = System.currentTimeMillis() / INTERVAL;
        // Give things time to report;
        Thread.sleep(INTERVAL * 2);
        long min = 99999;
        long max = 0;
        long total = 0;
        int number = 0;
        while (true) {
            long now = System.currentTimeMillis();
            long lastInterval = currentInterval;
            currentInterval += 1;
            long count = remove(lastInterval);
            count = count * 1000 / INTERVAL; // Multiply by 1000 to get
                                             // reqs/sec
            if (lastChange != 0
                    && (lastChange + INTERVAL * 3) < now) {
                // We only want to print anything if things have had a
                // chance to change

                if (count < min) {
                    min = count;
                }
                if (count > max) {
                    max = count;
                }
                total += count;
                number++;
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(lastInterval * INTERVAL);
                String report = lastInterval + " "
                        + calendar.get(Calendar.HOUR_OF_DAY) + ":"
                        + calendar.get(Calendar.MINUTE) + ":"
                        + calendar.get(Calendar.SECOND) + " "
                        + percentage + "% " + count + " " + min + " "
                        + ((double) total / (double) number) + " "
                        + max;
                System.err.println(report);
                if (sf != null) {
                    sf.println(report);
                }
            } else {
                max = total = 0;
                min = 999999999;
                number = 0;
            }
            Thread.sleep(INTERVAL);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}
 
Example 20
Project: androidtools   File: DateUtils.java   View Source Code Vote up 2 votes
/**
 * Get months
 *
 * @param mills mills
 * @return months
 */
public static int getMonthInMills(long mills) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(mills);
    return calendar.get(Calendar.MONTH) + 1;
}