kirktoon1882
kirktoon1882

Reputation: 1231

In Android, how to create a Time value?

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.

Upvotes: 0

Views: 130

Answers (3)

sealz
sealz

Reputation: 5408

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.

Upvotes: 0

Hevski
Hevski

Reputation: 1871

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();

Upvotes: 0

Tom Sengelaub
Tom Sengelaub

Reputation: 359

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);

Upvotes: 2

Related Questions