0

I'm at loss of what to do and require some advice. My app currently uses the device's time instead of the "correct time". Is there any way I can display the "correct time" and not the device's time? For example, when the user changes the device's time, the textview's time follows the new device's time. However, i don't want that to happen.

The following is my codes:

//start time
private void updateTextView() {

    Calendar a = Calendar.getInstance();
    int year = a.get(Calendar.YEAR);
    int month = a.get(Calendar.MONTH) + 1;
    int date = a.get(Calendar.DATE);
    int hourOfDay = a.get(Calendar.HOUR_OF_DAY);
    int minute = a.get(Calendar.MINUTE);

    datetime_start = (TextView) findViewById(R.id.inputStartTime);
    datetime_start.setText(year + "-" + month + "-" + date + " " + String.valueOf(hourOfDay) + ":" + String.valueOf(minute) + ":00");

}

If possible, please advice me what to do. I'm a beginner in Java Coding and is currently coding my school project. Thank you.

1 Answers1

1

If by "correct" time you mean UTC time, then you need to get a Calendar instance with UTC like this:

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));

Usually time is localized (timezone adjusted) because that is what is typically needed.

You can look at the question/answer here for more info: How can I set a Calendar with UTC time?

Community
  • 1
  • 1
Jim
  • 10,172
  • 1
  • 27
  • 36