How to Compare Time in Java? [duplicate] - java

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to calculate time difference in java?
I have two Strings "10:00:00" and "14:00:00". I have converted them to Date Format. Now i want two compare the both time.Can anyone suggest me what to do....

use compareTo().
date1.compareTo(date2);
from java docs:
Returns:
the value 0 if the argument Date is equal to this Date; a value less than 0 if this Date is before the Date argument; and a value
greater than 0 if this Date is after the Date argument.
other examples

Check Date#equals, Date#after, Date#before
I preferrably use Calendar class for date related task.. It is quite to use them in some context..
You can find more helpful examples in this link

Before you do the comparison, as the timestamps are still in the String format, you need to convert them to java.util.Date and the simplest way is to assign an arbitrary date part to both time strings (e.g. concatenate both time strings to "1/1/1970 "), then convert them into Date using SimpleDateFormat, then get time in milliseconds and do the comparison.

Related

return date type with format in java [duplicate]

This question already has answers here:
Change the format of Date Java [closed]
(2 answers)
Closed 4 years ago.
I'm trying implement get method for variable has type "Date".
But when return, I want to return it with format "yyyy/MM/dd" and MUST be Date type.
Example: original date: 2018/01/01T15:00.00.000+0000
I want return: 2018-01-01 and MUST be date, not string
Can you help me in this case ?
A Date is basically stored as a number of milliseconds since the epoch (1/1/1970). It can be formatted into a String in various ways (e.g. with SimpleDateFormat), but it is never stored as a String. So, what you're asking for is impossible and makes no sense.

Transform a String in unix time java android [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 6 years ago.
At the moment I have a String with the folowing format :
'DD/MM/YYYY' , and I'm willing to store this in a SQLite database as a date.
Since it is impossible to store it as a datetime, I've decided Integers would be the best choice, and someone told me about the Unix epoch solution. The thing is that I'm very unfamiliar with that, and I can't seem to convert a String into a unix epoch time...
Is there a way to directly convert a String with my format into a unix epoch time, or am I doing it wrong and should I change something?
I've read this question :Unix epoch time to Java Date object But still can not find my way out with my String...
Take a look at Parsing String date to date and adjust the format to yours.
If you have a java.util.Date just use http://docs.oracle.com/javase/6/docs/api/java/util/Date.html#getTime() to get the UNIX timestamp as long.
If in doubt, read the docs! ;)
https://developer.android.com/reference/java/text/SimpleDateFormat.html
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
sdf.parse(<value>);

get today's date and time as string [duplicate]

This question already has answers here:
How to get current moment in ISO 8601 format with date, hour, and minute?
(23 answers)
Calendar date to yyyy-MM-dd format in java
(11 answers)
Closed 6 years ago.
I am new to Java and trying to use Calendar object to get date and time of now as a string. I am particularly stuck at object and object conversions.
Here is the format I need (as a string):
2016-03-30T14:21:00Z
If I could just get the date and time format right, I could play around with the string but I am struggling with deprecated methods.
Thank you for replies
Your best bet is to start using Java 8's new Time API (or JodaTime if you can't use Java 8)
LocalDateTime now = LocalDateTime.now();
String isoFormat = DateTimeFormatter.ISO_INSTANT.format(now.toInstant(ZoneOffset.UTC));
System.out.println(isoFormat);
outputs 2016-03-30T17:51:38.639Z (when I tested it)
Solved my question using this link:
http://beginnersbook.com/2013/05/current-date-time-in-java/
Thanks for replies, I will also look into Java 8' time API

Difference between 2 dates Java [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Difference in days between two dates in Java?
I need to find difference between 2 dates. First one is in string format,
s1=2012-10-01T15:33:34.652905Z
I need to convert s1 into date d1 and then find the difference in integer between today's date d2 and d1.
How do I do this?
To convert your string to a date, you can use SimpleDateFormat.parse("yyyy-MM-dd'T'HH:mm:ss.SSSZ").
Given two Date objects, you can get whatever differences (days, seconds...) you need with JODA Time as advised in SO entry : Difference in days between two dates in Java?

Comparing 2 dates using following format yyyy:MM:dd hh:mm:ss

I need to compare two dates along with time portion.
I need to check it upto hh:mm:ss only.
Can any one suggest any util i can use for it or any suggestion for doing programatically.
Thanks,
Narendra
To compare just the date portion, the simplest thing to do is
int cmp = date1.substring(0, 10).compareTo(date2.substring(0,10));
You don't need to convert them into a true date object as the strings will be in the same order. This is like #rodion's answer except his will compare the time as well.
If performance IS important to you I suggest you leave the strings as strings, converting them to a date object is relatively expensive.
You could use a java.text.SimpleDateFormat to parse your dates into Date objects, then use getTime to do the comparison. Date also implements Comparable so you can use that directly if you prefer.
You can use string comparison like: "2010:01:01 00:00:01".compareTo("2010:01:01 00:00:02"). This will return -1, 0 or 1 for when the first date is before, same or after the second date respectively
If you have the Date or Calendar object available you can compare dates using Calendar.before(date) and Calendar.after(date) methods, but this will take into account milliseconds so you have to be careful to reset milliseconds to 0 before comparing.
If performance is not so important to you I would suggest string comparison above.

Categories