Check is new day/midnight java android - java

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
}

Related

Count up timer in java not starting from 0

I created a count up timer function to use in a java swing window. The problem is time does not start counting from zero. When I start the timer, the initial time always comes with an hour ahead.
Here's my code:
public static void timeRecording(){
Date startTime = new Date();
int delay = 1000; //milliseconds
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm:ss");
Date actualTime = new Date();
String dateToPrint = timeFormat.format(new Date(actualTime.getTime() - startTime.getTime()));
//String dateToPrint = timeFormat.format(actualTime);
System.out.println(actualTime);
//actualTime
// String timeToPrint.timeFormat = actualTime;
timerLabel.setText(dateToPrint);
}
};
new Timer(delay, taskPerformer).start();
}
This is the time shown at start:
Your SimpleDateFormat of "hh:mm:ss" uses h which, in the JavaDoc is described as:
h Hour in am/pm (1-12)
Thus your hour will always start at 1.
You could try using
K Hour in am/pm (0-11)
i.e. "KK:mm:ss"
What happens is that if the difference between the dates is one second, this: new Date(actualTime.getTime() - startTime.getTime()) creates a date at 1st of Jan 1970 at 00:01 UTC.
But when you format it, the DateFormat uses your time zone (Lisbon = UTC+1) and sees the date as 1st of Jan 1970 at 01:01 UTC+1.
If you want to get the correct output, you need to set the timezone of the formatter:
SimpleDateFormat fmt = new SimpleDateFormat("HH:mm:ss");
fmt.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(fmt.format(new Date(0))); //outputs 00:00:00 as expected
Note: The correct format pattern is HH (0-23), not hh (which is 1-12).

Java get a current time in 24h format

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

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

Time in milliseconds calculation

I have two string variables for timer i.e.
String StartTimer1 = "00:00:00";
String EndTimer2 = "23:59:59";
Now , I need to calculate the time left from the present time (lets say if it is 05: 30:00) once the timer has started and time left has to be in milliseconds.
Here some tipps to get started:
You can parse the String to a Java Date like this:
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
Date startTimer1Date = format.parse(StartTimer1);
You can get the current Date and Time like this:
Date dateNow = new Date();
And since you are working with time only (and not with date and time), you will need to manipulate the Date, Month and Year of all values to a common base, e.g. :
Calendar c = Calendar.getInstance();
c.setTime(dateNow);
c.set(Calendar.YEAR, 1970);
c.set(Calendar.DAY_OF_YEAR, 1);
dateNow = c.getTime();

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

Categories

Resources