0

In my Android app, I'm using the Time class. I understand getting the current time like this:

    Time now = new Time();
now.setToNow();

but what I'm stumbling on is how to create a set value of 8pm in the Time class. It's not just: Time time8 = "2200";, because that's a String, and Time time8 = 2200; is an integer. So I'm stumped.

kirktoon1882
  • 1,221
  • 5
  • 24
  • 40

3 Answers3

2

There are multiple ways to that, I think the most easiest for you would be to just set it directly:

set(int second, int minute, int hour, int monthDay, int month, int year)

Calendar rightNow = Calendar.getInstance();
int day = rightNow.get(Calendar.DAY_OF_MONTH);
int month = rightNow.get(Calendar.MONTH);
int year = rightNow.get(Calendar.YEAR);

Time time8 = new Time();
time8.set(0,0,22,day,month,year);

But i would only do it like that if you really want to use Time otherwise Calendar is much more useful

Calendar calendar8= Calendar.getInstance();
calendar8.set(Calendar.SECOND, 0);
calendar8.set(Calendar.MINUTE, 0);
calendar8.set(Calendar.HOUR_OF_DAY,22);
Tom Sengelaub
  • 359
  • 2
  • 5
0

Could use the calendar class

Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY,5);
cal.set(Calendar.MINUTE,50);
cal.set(Calendar.SECOND,0);
cal.set(Calendar.MILLISECOND,0);

Date d = cal.getTime();
Hevski
  • 1,821
  • 1
  • 17
  • 26
0

From android Dev.

A specific moment in time, with millisecond precision. Values typically come from currentTimeMillis(), and are always UTC, regardless of the system's time zone. This is often called "Unix time" or "epoch time".

You can do Date instead of time

 Date newdate = new Date();

You can use calendar to break it down to what you actually need.

sealz
  • 5,348
  • 5
  • 40
  • 70