Currently I have a timestamp for example say, 2012-06-23 14:24:07.975 and
two times - 8AM and 3PM.
In java, how can I check whether the above timestamp is between the particular two time.
In other words, I need to check whether the time in timestamp (2012-06-23 14:24:07.975) falls between 8AM and 3PM or not.
Any suggestions.
You can convert the timestamp into an actual Date afterwards you convert it into a Calendar from where you can extract the hour and check it against your given hours.
Use SimpleDateFormat for getting a Date out of your Timestamp, if you cannot directly convert it.. Afterwards
Calendar cal = new GregorianCalendar();
cal.setTime(yourDate);
int hour = cal.get(Calendar.HOUR_OF_DAY);
Now do your hour check.
before and after methods should help you to do that. See docs.
I suggest using compareTo method in Date.
Related
in Java I'm trying to compare two different hours by converting them to milliseconds if (20:00 > 19:30) // do anything. In millis, it would be if (72000000 > 70200000) // do anything
But the smartphone doesn't do it well. I'm storing the numbers in variables long, as in the class Calendar, the method myCal.getTimeInMillis() returns a long, but it doesn't work.
I have tried changing the data type of the variables from long to double and it does work, so I figure out that those large numbers simply "doesn't fit" in the variable, but then, why the Java Calendar method getTimeInMillis() returns a long?
How does time work in Java Calendar? Thanks in advance.
Edit:
Thank you for your answers and time, I'm sorry for this question because it does work to compare different hours which are stored in long variables. I have tried again and it does work (I don't know why it didn't work before). I'm making an alarm clock app for Android and I want to compare not only two hours (and minutes and seconds), but also day of the week and so on. So I'm going to mark as the solution of this question the answer of #Sufiyan Ghori because I think it can really help me, and I think I'm gonna delete this question because it has no sense.
I'm new here (and in programming in general), so sorry for this silly question.
instead of comparing hours after converting it into milliseconds you can use Date::before or Date::after methods after setting your time into two separate Date objects.
Date date1 = new Date(); // set time for first hour.
Date date2 = new Date(); // set time for second hour.
You can use Calendar to set time for each Date object, like this,
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY,12);
cal.set(Calendar.MINUTE,10);
cal.set(Calendar.SECOND,31);
cal.set(Calendar.MILLISECOND,05);
date1 = cal.getTime(); //set time for first hour
and then use these for comparison,
if(date1.after(date2)){
}
if(date1.before(date2)){
}
and Date::equals,
if(date1.equals(date2)){
}
getTimeInMillis() doesn't return milliseconds passed from the start of the current day, it returns millisecons passed since 01 Jan 1970 00:00:00 GMT.
Take a look at documentation of Calendar class: java.util.Calendar
Is there any way to update only a Date's time path?
I tried Date.setTime() but it replaces the date path too. I there any java method or the only way is to set hour, minute, second and milisecond?
Thank you
A Java Date is just a wrapper around a long that counts time from the epoch (January 1, 1970). Much more flexible is Calendar. You can create a Calendar from a Date:
Date date = . . .;
Calendar cal = new GregorianCalendar();
cal.setTime(date);
Then you can set various fields of the Calendar:
cal.set(Calendar.HOUR_OF_DAY, 8);
// etc.
I would start by moving away from java.util.Date entirely. Ideally, use Joda Time as it's a far more capable date/time library.
Otherwise, you should use java.util.Calendar. A java.util.Date doesn't have a particular date/time until you decide what time zone you're interested in - it just represents an instant in time, which different people around the world will consider to be a different date and time of day.
You'll want to take a look at java.util.Calender.
It will allow you to change the individual parts of the date/time.
Calendar cal = Calender.getInstance();
cal.setTime(date);
cal.set(Calender.HOUR, hour);
Alternatively, as has already being suggested, I'd take a look at Joda Time
I have a long data member that represents a date.
I cast it to a
Date d = new Date(long);
I want to now if a nother date has the same day.
How do I do it?
Thanks.
(For andrew)
Edit :
Found this solution
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
return fmt.format(date1).equals(fmt.format(date2));
in here
Comparing two java.util.Dates to see if they are in the same day
looks nice
use the joda api.
http://joda-time.sourceforge.net/
Its a lot easier and better than the Calendar object route in java jdk
Well you can convert them both to calendar Objects and get the calendar objects day and compare that way.
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(LONG VALUE HERE);
int day = cal.get(Calendar.DAY_OF_MONTH);
Do the same thing with the other date, and compare the values.
edit: By the way, you are not casting the long to a date, you are just creating a Date object using a long.
Use apache commons.
DateUtils.isSameDay(date1, date2);
To see if the dates are equal:
date_one.equals(date_two);
To see if just the day is equal, I usually chop the time off the date (setHours(0), setMinutes(0), etc.) and then use the .equals() method.
Use java.util.Calendar for all comparison operations.
I'm trying to put a user provided date into an SQL database, and I have the following lines to process the string and convert it to a java.sql.Timestamp object.
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd'T'hh:mm");
java.util.Date badDate = formatter.parse(request.getParameter("datetime"));
Timestamp date = new Timestamp(badDate.getTime());
The issue is, badDate is the correct date that the user input, but date always gets set to the wrong month and day, usually January 2, and the correct time and year. I had the same problem when I tried to convert to a java.sql.Date object, but then the time was set to midnight as well. I couldn't find anyone with a similar problem after some searching, maybe someone here has seen something like this?
The date format is wrong, it should be yyyy-MM-dd'T'HH:mm.
Please refer this document for details about date format.
m stands for Minutes
M for Months
h for hours in am/pm (1-12)
H for hours in 24 hour clock
It will be better if you can give a sample of the date value you are trying to parse.
You want "yyyy-MM-dd'T'hh:mm" (note capitalized MM). Otherwise your month is wrong in the format string.
I have a mobile application where I capture a date/time and send it as miliseconds to a servlet. -For example I capture 12:55 AM in my cellphone and send it to my server-
So, I construct the date from the sent miliseconds and print it with SimpleDateFormat but the time is diferent, it prints 8:55 AM.
I know that the problem is because I use 2 diferent timezones, my question is:
how can I show that time without apply any timezone to show the same time?
Thanks in advance
You need to use Calendar to change the TimeZone but there is no API for that.
// Change a date in another timezone
public static Date changeTimeZone(Date date, TimeZone zone) {
Calendar first = Calendar.getInstance(zone);
first.setTimeInMillis(date.getTime());
Calendar output = Calendar.getInstance();
output.set(Calendar.YEAR, first.get(Calendar.YEAR));
output.set(Calendar.MONTH, first.get(Calendar.MONTH));
output.set(Calendar.DAY_OF_MONTH, first.get(Calendar.DAY_OF_MONTH));
output.set(Calendar.HOUR_OF_DAY, first.get(Calendar.HOUR_OF_DAY));
output.set(Calendar.MINUTE, first.get(Calendar.MINUTE));
output.set(Calendar.SECOND, first.get(Calendar.SECOND));
output.set(Calendar.MILLISECOND, first.get(Calendar.MILLISECOND));
return output.getTime();
}
Link: http://blog.vinodsingh.com/2009/03/date-and-timezone-in-java.html
I think this should work. I haven't extensively tested it.
Calendar and Date objects store their date information in milliseconds in relation to UTC.
(the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC)
This means that Calendar/Date Objects do not store different values for different Time Zones. They always use the same value internally... the format is what normally changes.
Maybe you can use a SimpleDateFormat in your local/default timezone:
SimpleDateFormat sdf = new SimpleDateFormat("S")
Date d = sdf.parse(milliseconds);
You can also try to change the DateFormat's timezone until it matches your expected output.
sdf.setTimeZone(TimeZone.getTimeZone("GMT-8"));
System.out.println(sdf.toString());
sdf.setTimeZone(TimeZone.getTimeZone("GMT-4"));
System.out.println(sdf.toString());