Validity Comparision in java - java

Hey everyone in my web application i am generating a verification code that is to be entered by user for account activation. I want that the generated verification code should be valid for specific time period say 12 hrs or 24 hrs. I am storing details of date and time when code is generated but how can i check for validity means suppose user entered code how can i check that he has entered code in validation period or not? How to compare date and time together? Thanks

I wouldn't think of it as a date and time as such. I'd think of it as an instant in time. When you generate the verification code, work out the instant in time "24 hours from now", and store that. Then just check "the current instant" when you validate the verification code.
You could use System.currentTimeMillis() to get the instant as a long value in milliseconds. (It's since the Unix epoch, but that's actually irrelevant here.) In fact, I'd suggest you create a kind of Clock interface which can be used to determine the current instant, with an implementation using System.currentTimeMillis() - or the Joda Time equivalent. That way you can unit test your code more easily.
So take the current number of milliseconds, add your appropriate duration to it (again, in milliseconds), and that's your expiry instant.

Use a database such as my sql to store verification code. have a field called createdTime. When user tries to verify, subtract createdTime from now() to get less than 24.
RESPONSE: date subtraction
select something from someTable where createdTime >= date_sub(now(), interval 24 hour)

http://docs.oracle.com/javase/7/docs/api/java/util/Date.html
after() and before() methods do ecactly what you want.

The Java Date class has before() and after() functions for comparing dates.

You can use before or after of Calender object
For example:
// initialize ur date here
Date issueded = null;
Calendar issuedDate = Calendar.getInstance();
issuedDate.setTime(issueded);
Calendar expiredDate = Calendar.getInstance();
// minus 12 H
expiredDate.add(Calendar.HOUR, -12);
if(expiredDate.before(issuedDate)) {
// do ur thing
} else {
// expired
}

Related

Getting Timezone Information from Long value in java

Can we get from which timezone the Long Value is produced?
I have long values of Date. I want to know from which timezone it is generated.
For e.g
Long value: 1435640400000
Date: 30 June 2015 CDT
I want to develop program which input will be the Date in long value
that will return output as Timezone with the respective
long value for 30 June 2015 12:00 AM GMT/UTC
The unix time (as it is called) is not a date. You can calculate a date from it but it really is just the duration of seconds (or ms) since 01/01/1970 at 00:00 UTC.
This means it has no timezone attached to it. You need the "target" timezone to calculate the actual date from it, but simply having this number does not include any timezone information (which means you'll need to get it somewhere else in order to calculate dates).
Think of the unix timestamp more as a duration than a date. It's like saying "I'll meet you in 30 minutes". Those 30 minutes do not have a timezone attached to them. To you and the person you're talking to, that meeting might happen at different dates (e.g. 2:30pm vs. 3:30pm) because of timezones. But it will still happen at the same point in time relative to the moment you said it.
I hope this makes the difference somewhat clearer.
There was a way to do this directly via the getTimezoneOffset() function in the Date class but that has been deprecated.
It has been replaced by
(Calendar.get(Calendar.ZONE_OFFSET) + Calendar.get(Calendar.DST_OFFSET)) / (60 * 1000)

In which type of variable store time in millis? Choosing a correct data type

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

How to show date and time from milliseconds in Android?

I want to display the date of a trip. In the application several trips are listed, each has a long value with the currentTimeMillis of its generation. I want to turn this long number into a date but the output is always something around 1970... Is System.currentTimeMillis() the right way to store the time of the trip generation?
Date date = new Date(trip.getStartTime()); // 195342322
SimpleDateFormat test = new SimpleDateFormat("yyyy-MM-dd");
startTime.setText(String.valueOf(test.format(date)));
-> Output of startTime: 1970-01-01
Try to use Calendar for simple formatting
We don't know how trip.getStartTime() is implemented, however it returns a wrong value. 195342322 is Wed, 10 Mar 1976 21:45:22 GMT (you can check it online)
Since the only non-deprecated Date constructor accepts a long argument, I guess you used that. It's better to store the date(time) in a database-friendly way, such as ISO 8601. Note that if you store it on the client in a SQLite database, SQLite doesn't have a time type, and you have to use a text field for that.
Once you switch to the text representation for serialization, you will be able to get back a Date by using SimpleDateFormat.parse()
use something like this:
Calendar calendar=Calendar.getInstance();
calendar.setTimeInMillis(trip.getStartTime());
Date date =calendar.getTime ();
as shown here .

Set Time Without Knowing Date

I want to be able to store time values in a mysql database without actually knowing a specific date they are associated with. I am using the java.sql.time to store the data.
long timeInMillis = 43200000; //This is 12 hours in milliseconds.
Time time = new Time(timeInMillis);
For whatever reason this is giving me a time of 07:00:00 and it should be 12:00:00. I'm assuming this is because when setting a time variable it is based on the amount of time past a specific date. How do I set a time variable without actually using a pre-defined date?
The reason I am doing this is because I want to store a week day and a time range in a database. So Sunday between 12 and 1 would be 0 between 12:00:00 and 13:00:00. I want to be able to compare an actual date value against the database and see if the date falls between the time periods based on the dates day of week regardless of the month or year. Storing full dates in the database for each possible weekday and time would result in thousands of unnecessary entries.
For whatever reason this is giving me a time of 07:00:00 and it should be 12:00:00. I'm assuming this is because when setting a time variable it is based on the amount of time past a specific date
Can happen due to TimeZone difference. Check for timezone information while saving and retrieving values.
This is likely a timezone issue, but I solved the problem by passing the time values into the database as a string. It is probably best to use a physical string in this instance anyways unless timezone properties are important.
String startTime = "12:00:00";
String endTime = 01:00:00;
If timezone is important I assume the best method to use unless you are preparsing your date object would be to set the timezone directly in your mysql statement for the current session. More details can be found here: How do I set the time zone of MySQL?
EDIT: Turns out in the end this wasn't actually a time zone issue (although in other circumstances it could be, so don't discredit adjusting for time zone). I was calculating the time in milliseconds incorrectly.

Java checking time in a timestamp

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.

Categories

Resources