I have the two different timestamp values as String. I need to find which one is latest. Its in the format of [YYYYMMDDHHMMSS]
Timestamps are :
20150804030251
20150804040544
Is there any easy way to get the latest using Java 8?
I have the two different timestamp values as String. I need to find which one is latest. Its in the format of [YYYYMMDDHHMMSS]
Timestamps are :
20150804030251
20150804040544
Is there any easy way to get the latest using Java 8?
Yes, those timestamps are formated in a way which is easy to compare.
if ( Long.parseLong(timesteamp1) < Long.parseLong(timestamp2) ) {
//timestamp2 is later than timestamp1
}
This is possible because the most significative part, the year, is in the most significative part of an integer, the leftmost; the rest of the parts go in decreasing order of significance from left to right; and a fixed number of digits is used for each part, like month 02 instead of month 2. Otherwise this simple way would not be possible.
You can also compare them lexicografically. The previous code, in the particular case of this format, is equivalent to :
if ( timestamp1.compareTo(timestamp2) < 0 ) {
// timestamp2 is later than timestamp 1
}
you can create local date object In java 8, like below
LocalDateTime dt = LocalDateTime.parse("20150804030251",
DateTimeFormatter.ofPattern("YYYYMMDDHHMMSS")
LocalDateTime dt2 = LocalDateTime.parse("20150804030251",
DateTimeFormatter.ofPattern("YYYYMMDDHHMMSS")
then compare using dt.isBefore(dt2)
You can still try this. In here not use any special feature in Java 8
String timeStamp1 = "20150804030251";
String timeStamp2 = "20150804040544";
DateFormat dateFormat = new SimpleDateFormat("yyyyMMhhHHmmss");
Date date1 = dateFormat.parse(timeStamp1);
Date date2 = dateFormat.parse(timeStamp2);
if(date1.after(date2)){
System.out.println("latest "+date1);
}else {
System.out.println("latest "+date2);
}
Unfortunately, none of the existing answers is proper. Let's first discuss the problem with them:
MMyyyy...
. Another serious problem with this approach is that it will fail to validate a wrong value e.g. 15 digits instead of 14 digits.Y
(which specifies week-based-year) instead of y
(which specifies year-of-era) and D
(which specifies day-of-year) instead of d
(which specifies day-of-month). In fact, it is recommended to use u
instead of y
. Check the documentation page
to learn more about these symbols.Date(long date)
should initialize the object with the no. of milliseconds since 1970-01-01T00:00:00Z
.By now, you must have already figured out the solution which I have posted below just for the sake of completeness:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
String strDateTime1 = "20150804030251";
String strDateTime2 = "20150804040544";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuuMMddHHmmss");
LocalDateTime ldt1 = LocalDateTime.parse(strDateTime1, dtf);
LocalDateTime ldt2 = LocalDateTime.parse(strDateTime2, dtf);
LocalDateTime latest = ldt1.isAfter(ldt2) ? ldt1 : ldt2;
System.out.println(latest);
}
}
Output:
2015-08-04T04:05:44
Learn more about the the modern date-time API* from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.