I use the Time class to get the current time, and I need to get it in the mm.hh format. To do this, I use the following approach:
Time time = new Time();
time.setToNow();
String currentTime = time.hour + ":" + time.minute;
Can I increase the current time, for example, by one hour in this case?
Time time = new Time();
time.setToNow();
++time.hour; // increase 1 hour
time.normalize(false);
First increase your hour and then format it again:
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR, 1);
String currentTime = calendar.get(Calendar.HOUR) + ":" + calendar.get(Calendar.MINUTE);
if you need hour in 24-format use Calendar.HOUR_OF_DAY
Try this:
Time time = new Time();
time.setToNow();
time.hour++;
time.toMillis(false);
Related
Time class is no longer possible to use.
I want to ask you, how to detect in app 3-4am?
I need that to set up for example night mode in my app.
Can you give me some example how to do it?
Instead of using Time (because Time class was deprecated in API level 22.) you can use Calendar for getting current hour
val rightNow = Calendar.getInstance()
val currentHourIn24Format: Int =rightNow.get(Calendar.HOUR_OF_DAY) // return the hour in 24 hrs format (ranging from 0-23)
val currentHourIn12Format: Int = rightNow.get(Calendar.HOUR) // return the hour in 12 hrs format (ranging from 0-11)
We can use the Calendar class to get a format like "HH:mm:ss"
Calendar calendar = Calendar.getInstance();
int hour24hrs = calendar.get(Calendar.HOUR_OF_DAY);
int hour12hrs = calendar.get(Calendar.HOUR);
int minutes = calendar.get(Calendar.MINUTE);
int seconds = calendar.get(Calendar.SECOND);
System.out.println("Current hour 24hrs format: " + hour24hrs + ":" + minutes +":"+ seconds);
System.out.println("Current hour 12hrs format: " + hour12hrs + ":" + minutes +":"+ seconds);
Other option using the Date class and applying the format "HH:mm:ss":
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
Date date = new Date();
String dateformatted = dateFormat.format(date);
System.out.println(dateformatted);
You can use following methods:
SimpleDateFormat format = new SimpleDateFormat("HH", Locale.US);
String hour = format.format(new Date());
Calendar calendar = Calendar.getInstance();
int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
public static final String inputFormat = "HH:mm";
private Date date;
private Date dateCompareOne;
private Date dateCompareTwo;
LINE 5:
private String compareStringOne = String.valueOf(SetTimeActivity.intFromTimeH)+ ":"+ String.valueOf(SetTimeActivity.intFromTimeM) ;
LINE 6:
private String compareStringTwo = String.valueOf(SetTimeActivity.intToTimeH) + ":"+ String.valueOf(SetTimeActivity.intToTimeM);
SimpleDateFormat inputParser = new SimpleDateFormat(inputFormat, Locale.US);
private void compareDates()
{
Calendar now = Calendar.getInstance();
int hour = now.get(Calendar.HOUR_OF_DAY);
int minute = now.get(Calendar.MINUTE);
date = parseDate(hour + ":" + minute);
dateCompareOne = parseDate(compareStringOne);
dateCompareTwo = parseDate(compareStringTwo);
if (!(dateCompareOne.before( date ) && dateCompareTwo.after(date))) {
....
I am trying to check if current time falls between the specified time. For that I am converting the specified time into strings first (in Line5 & Line6). Even though I get the integer values correct, the string formed always shows "0:0".
Also, the year is shown as 1970 (The date & the day shown are wrong as well).
I need to get the current time. What am I doing wrong?
private Date parseDate(String date) {
try {
return inputParser.parse(date);
} catch (java.text.ParseException e) {
return new Date(0);
}
}
The parseDate() function returns the time elapsed since the 1st of January 1970. This is known as the Unix Epoch, and it's how all time is represented in Unix computers. By running the parseDate function on a string containing just hours and minutes, you're creating a Date object which represents a time HH:mm past the first of January 1970.
Your code is using a really odd way of getting the current time. Converting a Calendar to two ints, then to a string and finally parsing back to a Date is going to be inefficient and open you up to all sorts of needless errors.
When you initialise a new Date object it is automatically assigned the time of initialisation. Therefore:
Date d = new Date();
would result in d being the moment of initialisation (that is, this year, month, day, hour, minute, second and microsecond). Then you can just use Date.after() and Date.before().
If you still want to do it via the Calendar method, then you'd be better served by:
cal = Calendar.getInstance();
Date d = cal.getTime();
It may be that you've got other issues, but it's worth doing it properly first. When you pass data by writing it as a string (especially when it's time related, with all sorts of ambiguities about what "12" actually represents) you lose all the advantages that language typing gives you.
this code help you
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE); if (c.get(Calendar.AM_PM) == Calendar.AM)
am_pm = "AM";
else if (c.get(Calendar.AM_PM) == Calendar.PM)
am_pm = "PM";
// Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");
String formattedDate = df.format(c.getTime());
Toast.makeText(this, formattedDate, Toast.LENGTH_SHORT).show();
If you already work with Date objects why not using the Date.after(...) and Date.before(...) methods.
I must convert the actual date and time to millis and into other timezone GMT+3 (my timezone is GMT-2). I use this code but it return me hte time but into my timezone....why ?
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT-3"));
cal.get(Calendar.DAY_OF_MONTH);
cal.get(Calendar.MONTH);
cal.get(Calendar.YEAR);
cal.get(Calendar.HOUR_OF_DAY);
cal.get(Calendar.MINUTE);
cal.get(Calendar.SECOND);
cal.get(Calendar.MILLISECOND);
long timez = cal.getTime().getTime();
You need to use SimpleDateFormat. Calendar always uses default configured timezone on your machine. Here is example on how to achieve this functionality with SimpleDateFormat.
Date date = new Date();
DateFormat firstFormat = new SimpleDateFormat();
DateFormat secondFormat = new SimpleDateFormat();
TimeZone firstTime = TimeZone.getTimeZone(args[0]);
TimeZone secondTime = TimeZone.getTimeZone(args[1]);
firstFormat.setTimeZone(firstTime);
secondFormat.setTimeZone(secondTime);
System.out.println("-->"+args[0]+": " + firstFormat.format(date));
System.out.println("-->"+args[1]+": " + secondFormat.format(date));
}
where arg[0] and arg1 are the two time zone.
Refer this LINK
The getTime() method return the same time. it has no relation with the timezone.
I hope someone can help. Im trying to set up a timer that times from the start of a game and displays this time. The problem is that the following section of code gives me the wrong time. Its in the wrong format, and is out by an hour.
private long startTime;
private SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss.SS");
//Constructor:
startTime = System.currentTimeMillis();
public String getTime() {
long gameTime = System.currentTimeMillis() - startTime;
final Date date = new Date(gameTime);
return timeFormat.format(date);
}
It consistently gives me the output of 01:00:03:203. The seconds are correct, but the 1 hour shouldn't be there, and for format is 3 decimal places instead of the two I thought I specified.
Thank you very much!
Your date is epoch + gameTime. I think you're experiencing a daylight saving shift since the current DST in your location today doesn't match the DST at epoch.
Use a Calendar instead of a Date. Start with today and explicitly wipe out the hour, minute, etc. parts:
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 3600000 + 60000 + 1000 + 1);
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss.SS");
System.out.println(timeFormat.format(cal.getTime()));
The output for the above is: 01:01:01.01
http://ideone.com/DyQcl
Substitute the numbers I have above with gameTime and you're done.
Of course, this may not work once your millisecond ticks exceed the day boundary.
I have a program which should display the current time. It displays the right time immediately after I install it to a phone. But after that, whenever I run the application, the time doesn't change at all.
Here's my code:
Calendar cal = new GregorianCalendar();
hour = cal.get(Calendar.HOUR);
minute = cal.get(Calendar.MINUTE);
second = cal.get(Calendar.SECOND);
year = cal.get(Calendar.YEAR);
month = cal.get(Calendar.MONTH)+1;
day = cal.get(Calendar.DATE);
date = day+"_"+month+"_"+year+"_";
Current_Time = date+ hour + "_" + minute + "_" + second;
Shekhar... I had the same misunderstanding. When you create an instance of calendar, it represents the date at which it was instantiated and that state does not change! So, to get the current "date" you need to create a new instance.
JAL
You can write it as a method. Each call to create a new instance of the Calendar