• Post Reply Bookmark Topic Watch Topic
  • New Topic

Date and time range in Java  RSS feed

 
Mckenzie John
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello experts,

I have a requirement to query the database for a particular set of records. the query is based on specific date interval. The date from the front end is going to be "mm/dd/yyyy" format. But i need to convert it to timeinmilliseconds format to query the timestamp.

I face the following challenge:

When i select the dates i should be able to search the database from 00:00:00 of the start date to 23:59:59 of the end date for a valid case. How do i get this done. I am not able to find out how to add 23:59:59 to the end date formatted.

Need help here.

My piece of code:

SimpleDateFormat format1 = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat format2 = new SimpleDateFormat("dd-M-yyyy HH:mm:ss");
SimpleDateFormat format3 = new SimpleDateFormat("dd-M-yyyy 23:59:59");
Date date = format1.parse(fromUI);
String convertedFormatStartString = format2.format(date);
String convertedFormatEndString = format3.format(date);

System.out.println(convertedFormatStartString);
System.out.println(convertedFormatEndString);

DateFormat format = new SimpleDateFormat("dd-M-yyyy HH:mm:ss");
Date finalStartDate = format.parse(convertedFormatStartString);
System.out.println("P"+finalStartDate);

System.out.println(finalStartDate.getTime());

DateFormat eformat = new SimpleDateFormat("dd-M-yyyy 23:59:59");
Date finalEndDate = eformat.parse(convertedFormatEndString);
System.out.println(finalEndDate);

System.out.println(finalEndDate.getTime());

-----------------------------

OUTPUT:
30-9-2015 00:00:00
30-9-2015 23:59:59
PWed Sep 30 00:00:00 PDT 2015 ------------ Start
1443596400000
Wed Sep 30 00:00:00 PDT 2015 ------------ END (this needs to be 23:59:59 further from the 00:00:00)
1443596400000

Please guide
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66263
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two things:
  • You shoud use ISO format for the date.
  • It's easy to figure out what 24 hours is in milliseconds, no? Or, look into Calendar.

  •  
    Mckenzie John
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Can you give me a code snippet?
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 66263
    151
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    (Assumes not using Java 8)

    Dates are stored as long values in milliseconds. Adding 24 hours (converted to milliseconds) gets you 24 hours in advance. The Calendar class has an add() method to do the same.

    The javadoc for java.util.Date shows you how to get the long value, and how to create a new Date from a long value.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 66263
    151
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    P.S. This:
    from 00:00:00 of the start date to 23:59:59
    isn't correct. With this approach you miss the last second of the day.
     
    A.J. Côté
    Ranch Hand
    Posts: 417
    Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Bear Bibeault wrote:(Assumes not using Java 8)

    Dates are stored as long values in milliseconds. Adding 24 hours (converted to milliseconds) gets you 24 hours in advance. The Calendar class has an add() method to do the same.

    The javadoc for java.util.Date shows you how to get the long value, and how to create a new Date from a long value.


    Hi Bear,

    It is a little more complex than that. Computing time gaps accurately is comparable to the Holy Grail and please don't get me started about Joda time ;-)

    I will only say that I would use Calendar concrete classes anytime.

    Actually, the add() method is abstract in java.util.Calendar. I always recommend against 86400 when time computation needs to be as accurate as possible.

    For, example, class GregorianCalendar takes into account leap years in its add() method while Unix time ignores leap seconds completely, leaving to the application layer the burden of finding out the actual accurate time.


    https://en.wikipedia.org/wiki/Unix_time#Leap_seconds
     
    Campbell Ritchie
    Marshal
    Posts: 56223
    171
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    A.J. Côté wrote:. . . please don't get me started about Joda time ;-)
    Joking apart, what is wrong with Joda time?
    I will only say that I would use Calendar concrete classes anytime. . . .
    I am very grateful that there are decent time classes now available and it is no longer necessary to use Calendar and its subclasses at all in new code.
     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!