1

currentTiming: 16-02-21 8:22, dbTiming: 15-02-21 11:22

import java.util.*;
import java.text.SimpleDateFormat;
public class Main
{
    public static void main(String[] args) {
        SimpleDateFormat sf = new SimpleDateFormat("dd-MM-YY HH:mm");
        Date currentTimeandDate = new Date(System.currentTimeMillis());
        String dbTiming = "15-2-21 11:34";
        String currentTiming = sf.format(currentTimeandDate);
        
        try{
                Date date1 = sf.parse(dbTiming);
                Date date2 = sf.parse(currentTiming);
                boolean isBefore = date1.before(date2);
                // int val = date1.compareTo(date2);
                System.out.println(isBefore +" "+ currentTiming);  
                
        }catch(Exception e){
            
        }
    }
}

Why it's returning false, although dbTiming is before from currentTiming.. Where the code is wrong ?

Bhaskar Jha
  • 101
  • 1
  • 8
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Feb 16 '21 at 11:32
  • @Ole V.V. yes boss, i changed to DateFormat.format("hh:mm aa",calendar); – Bhaskar Jha Feb 16 '21 at 14:47
  • Well, that is not a method that I know and not a part of the java.time API that I recommended. I’d use (and adapt as necessary) the code from [the answer by Basil Bourque](https://stackoverflow.com/a/66218794/5772882). – Ole V.V. Feb 16 '21 at 18:29

3 Answers3

2

Its yy for the year field instead of YY. This is the correct format :

SimpleDateFormat sf = new SimpleDateFormat("dd-MM-yy HH:mm");
Akshar
  • 121
  • 3
2

tl;dr

LocalDateTime                                       // Represent a date with time-of-day, but lacking the context of a time zone or offset-from-UTC. So, this is *not* a moment. 
.parse( 
    "15-2-21 11:34" , 
    DateTimeFormatter.ofPattern( "d-M-uu HH:mm" )  
)                                                   // Returns a `LocalDateTime` object.
.atZone(                                            // Determine a moment by placing the date & time within the context of a time zone.
    ZoneId.of( "Africa/Tunis" )                     // The time zone by which to interpret the input string. Do this only if you are *certain* of the intended zone.
)                                                   // Returns a `ZonedDateTime` object.
.toInstant()                                        // Same moment, as viewed through an offset of zero hours-minutes-seconds from UTC.
.isAfter(                                           // Compare two moments.
    Instant.now()                                   // Capture current moment as seen through an offset of zero hours-minutes-seconds from UTC.
)                                                   // Returns a boolean.

Details

Besides the solution posted by Akshar, you have other problems.

Your example input has a single digit for month, but your formatting codes use two MM characters which means you expect always two digits with padding zero if needed. Use one M and one d if you do not expect padding zero.

You are using classes that represent a date-time in the context of a time zone or offset. But your input lacks any indicator of a time zone or offset.

You are using terrible date-time classes that were supplanted years ago by the modern java.time classes.

String input = "15-2-21 11:34" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "d-M-uu HH:mm" ) ;
LocalDateTime ldt = LocalDateTime.parse( input , f ) ;

You are trying to compare that date-time with the current moment. That does not make sense, as that text input does not tell us the time zone in which we are expected to perceive that date and time. Is that 11:30 AM in Tokyo Japan, 11:30 AM in Toulouse France, or 11:30 AM in Toledo Ohio US? Those would be three different moment, several hours apart.

If you know for certain the intended time zone, apply a ZoneId to get a ZonedDateTime.

ZoneId z = ZoneId.of( "America/Edmonton" ) ;
ZonedDateTime zdt = ldt.atZone( z ) ;
ZonedDateTime now = ZonedDateTime.now( z ) ;
if( zdt.isBefore( now ) ) { … }
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • you're right, but in my case, my App is useful for only my country, But i appreciate that you discussed about TimeZone as well, will help in future for me and others also. – Bhaskar Jha Feb 16 '21 at 14:45
0

the format you are using is not right:

https://docs.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html

here the demo

public class Main
{
    public static void main(String[] args) {
        SimpleDateFormat sf = new SimpleDateFormat("dd-MM-yy HH:mm");
        Date currentTimeandDate = new Date(System.currentTimeMillis());
        String dbTiming = "15-2-21 11:34";
        String currentTiming = sf.format(currentTimeandDate);
        
        try{
                Date date1 = sf.parse(dbTiming);
                Date date2 = sf.parse(currentTiming);
                System.out.println("date1: " + date1);  
                System.out.println("date2: " + date2);  
                boolean isBefore = date1.before(date2);
                // int val = date1.compareTo(date2);
                System.out.println(isBefore +" "+ currentTiming);  
                
        }catch(Exception e){
            
        }
    }
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97