Java get a current time in 24h format - java

Now i getting a current time
Calendar c = Calendar.getInstance();
int hours = c.get(Calendar.HOUR);
int minutes = c.get(Calendar.MINUTE);
int seconds = c.get(Calendar.SECOND);
and hours is always retrieving in 12h format. How i can change my code to getting actual hours in 24h format?

Use Calendar.HOUR_OF_DAY that is used for the 24-hour clock.
int hours = c.get(Calendar.HOUR_OF_DAY);

Related

How to get the timezone offset in android?

I found some similar questions, such as:
How to get the timezone offset in GMT(Like GMT+7:00) from android device?
How to find out GMT offset value in android
But all these answers(+12:00) are incorrect for New Zealand Daylight Saving Time now.
When I did debug, I got this from Google Calendar event object:
"dateTime" -> "2016-11-06T10:00:00.000+13:00"
So how to get the correct offset which should be +13:00?
Thanks.
To get the current offset from UTC in milliseconds (which can vary according to DST):
return TimeZone.getDefault().getOffset(System.currentTimeMillis());
To get a RFC 822 timezone String instead, you can simply create a SimpleDateFormat instance:
return new SimpleDateFormat("Z").format(new Date());
The format is (+/-)HHMM
So, I tried to get gmt offset through Calendar and SimpleDateFormat but both returns 0. I found the solution using deprecated methods in Date class.
So, this code works for me.
private double getOffset() {
Calendar calendar = Calendar.getInstance(Locale.ENGLISH);
int defHour = calendar.get(Calendar.HOUR_OF_DAY);
int defMinute = calendar.get(Calendar.MINUTE) + (defHour * 60);
Date date = new Date(System.currentTimeMillis());
int curHour = date.getHours();
int curMinute = date.getMinutes() + (curHour * 60);
double offset = ((double) curMinute - defMinute) / 60;
return offset > 12? -24 + offset : offset;
}
Then you can format a result
This code return me GMT offset.
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.getDefault());
Date currentLocalTime = calendar.getTime();
DateFormat date = new SimpleDateFormat("Z");
String localTime = date.format(currentLocalTime);
It returns the time zone offset like this: +0530

How to convert time into milliseconds?

I am very confused on how I can convert a given time like 9:30pm into milliseconds because I need to run a code if it is past a certain time. I already know how to get the current time in milliseconds by the following code:
long timestamp = System.currentTimeMillis();
But how would I convert 9:30pm into milliseconds? I have been researching for hours now and I can only seem to find out how to get the current time.
My application needs to check if it is 9:30pm or past and if so, run a toast message.
The fastest and correct way to do it on Android is to use Calendar. You can make Calendar instance static and reuse it whenever you need it.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR, 9);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.AM_PM, Calendar.PM);
long timeInMillis = calendar.getTimeInMillis();
I do not need to check time in milliseconds, you can compare current time with desired values using Calendar class:
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
if (hour > 21 || (hour == 21 && minute >= 30)) {
doSomeJob();
}
Note that this code will not work after a midnight.
If you need time in milliseconds for 9:30pm today, you should use Calendar object to build date and time you need.
// init calendar with current date and default locale
Calendar cal = Calendar.getInstance(Locale.getDefault());
cal.setTime(new Date());
// set new time
cal.set(Calendar.HOUR_OF_DAY, 21);
cal.set(Calendar.MINUTE, 30);
cal.set(Calendar.SECOND, 0);
// obtain given time in ms
long today930PMinMills = cal.getTimeInMillis();
No need for milliseconds if you have a decent date-time library.
You can use the Joda-Time library on Android.
DateTime dateTime = new DateTime( 2014, 1, 2, 3, 4, 5 ); // Year, month, day, hour, minute, second.
boolean isNowAfterThatDateTime = DateTime.now().isAfter( dateTime );
Why don't you do it with a constant? You said that you need to check if is past 9;30. So convert that time to milliseconds and use it ad a constant. 21:30 = (21 * 60 + 30) * 60 * 1000 will give u the 9;30 in milliseconds to compare with the current time that u get in milliseconds

Check is new day/midnight java android

I have timer witch is triggered periodically every 30 minutes - how to check on each trigger is current time midnight (is it new day) ?
I tried something like this
SimpleDateFormat parser = new SimpleDateFormat("HH:mm:ss");
Date date = new Date();
Date currTime = parser.parse( parser.format( date ) );
but I am not sure how to check if it is midnight, because I don't know does it use 24:00:00 or 00:00:00 for midnight clock, so I can use it and then check if current time is between midnight and lets say midnight and one minute like this :
if( currTime.after(midnight) && currTime.before(midnight and one minute) ){...}
You may want to try :
Calendar c = Calendar.getInstance();
int hours = c.get(Calendar.HOUR_OF_DAY);
int minutes = c.get(Calendar.MINUTE);
int seconds = c.get(Calendar.SECOND);
if(hours*3600 + minutes*60 + seconds < 1800){
// Day changed since last task
}

How to get a last Week date in GWT

I am Using GWT.
I need to retrieve the current date and and last week date. and pass it to GWT server using RPC.
How to retrieve the system date and last week date.??
You will Get Date/Time in/with GWT.
get a unix time stamp since the epoch
get year, month, today, date, hours, minutes seconds
//Get the browsers date (!!!note: I can't get GMT time zone in eclipse debugger)
Date date = new Date();
int Month = date.getMonth();
int Day = date.getDate();
int Year = date.getYear();
int Hour = date.getHours();
int min = date.getMinutes();
int sec = date.getSeconds();
int tz = date.getTimezoneOffset();
int UnixTimeStamp = (int) (date.getTime() * .001);//get unix time stamp example (seconds)
Long lTimeStamp = date.getTime(); //time in milleseconds since the epoch
int iTimeStamp = (int) (lTimeStamp * .001); //(Cast) to Int from Long, Seconds since epoch
String sTimeStamp = Integer.toString(iTimeStamp); //seconds to string
//get the gmt date - will show tz offset in string in browser, not eclipse debug window
String TheDate = date.toString();
//render date to root panel in gwt
Label label = new Label(TheDate);
RootPanel.get().add(label);
****** other wise Visit following link to get more information
1)a GWTInfo
2)one more a Stack
I hope it will help.
I make it using calculation by stander date util
you can find the date as below
Date fromday = new Date(System.currentTimeMillis() - 6000L * 60L * 60L * 24L);
Date today = new Date(System.currentTimeMillis());
Today will get current day, fromDay will get past 6 day

Calendar.MINUTE giving minutes without leading zero

Hi all I am using below code to get android phone time, but it is giving me minutes without zero if the minutes are in between 1 to 9.
for example:right now I have time on my device 12:09 but its giving me as 12:9
Calendar c = Calendar.getInstance();
int hrs = c.get(Calendar.HOUR);
int mnts = c.get(Calendar.MINUTE);
String curTime = "" + hrs + ":" + mnts;
return curTime;
after above code I also try below code its giving same thing as above, minutes without zero before number it the minutes in between 1 to 9 . .
final Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
Date date = cal.getTime();
int mHour = date.getHours();
int mMinute = date.getMinutes();
As Egor said, an int is just an integer. Integers don't have leading zeros. They can only be displayed with leading zeros when you convert them to String objects. One way to do that is like this:
String curTime = String.format("%02d:%02d", hrs, mnts);
The format string %02d formats an integer with leading zeros (that's the 0 in %02d), always taking up 2 digits of width (that's the 2 in %02d).
That would produce the String
12:09
All is said about integer. But dealing with dates and Calendars to display that information should be used like so:
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yourpattern"); //like "HH:mm" or just "mm", whatever you want
String stringRepresentation = sdf.format(date);
the pattern "mm" will have a leading zero if it is between 0 and 9. If you use "mmmm" you'll get 0009, which doesn't look like it makes a lot of sense, but it all depends on what you want. :)
if you use pattern HH:mm you'll get 12:09 (the current time of your date instance).
You can format date using SimpleDateFormat class
if you are getting date and time from Calendar instance:-
final Calendar c = Calendar.getInstance();
SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm");
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
timeEdittext.setText(timeFormat.format(c.getTime()));
dateEdittext.setText(dateFormat.format(c.getTime()));
if you have day, month, year, hour, minute as integer(usually happens in datepicker and timepicker dialogs)
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, dayOfMonth);
String date = new SimpleDateFormat("dd-MM-yyyy").format(calendar.getTime());
Used below method to convert Hour:Minute in double format.
/*
* make double format hour:minute string
* #hour : 1
* #minute : 2
* return : 01:02
* */
public static String hourMinuteZero(int hour,int mnts){
String hourZero = (hour >=10)? Integer.toString(hour):String.format("0%s",Integer.toString(hour));
String minuteZero = (mnts >=10)? Integer.toString(mnts):String.format("0%s",Integer.toString(mnts));
return hourZero+":"+minuteZero;
}
function time()
{
var time = new Date();
var hh = time.getHours();
var mmm = time.getMinutes();
if(mmm<10)
{
mmm='0'+mmm
}
time = 'T'+hh+':'+mmm;
return time;
}

Categories

Resources