Skip to main content

developerWorks >  Java technology  >  Forums  >  Java filter  >  developerWorks

Date without time    Point your RSS reader here for a feed of the latest messages in this thread


     

 
 

My developerWorks
 Welcome, Guest
Sign in or register
Permlink Replies: 2 - Pages: 1 - Last Post: Oct 23, 2009 2:22 AM Last Post By: Lu Wei Threads: [ Previous | Next ]

Posts: 3
Registered: Jul 28, 2005 10:24:12 AM
Date without time
Posted: Mar 14, 2007 04:08:41 PM
Click to report abuse...   Click to reply to this thread Reply
How can I get a Date that is just a date for comparison? When I create a Date object it includes the time. I only want month, day, and year. Can I do it without a Calendar object?
Joe Sam Shirah

Posts: 931
Registered: Dec 16, 2002 02:42:10 PM
Re: Date without time
Posted: Mar 15, 2007 12:13:11 PM   in response to: in response to: 's post
Click to report abuse...   Click to reply to this thread Reply

You can use java.sql.Date, but it's such a thin wrapper over java.util.Date that it still requires manipulation to get just a date. Effectively, what you want is all time elements to be zero. Another unfortunate aspect is that java.sql.Date has no default ( no arg ) constructor.

You could use various divisions, but probably the most straightforward way takes advantage of the API-specified format for java.sql.Date.toString(). That is JDBC escape format yyyy-mm-dd. It's useful here because java.sql.Date.valueOf() will generate a Date using that format. Since the JDBC date escape format has no time element, you get just the date. Because java.sql.Date descends from java.util.Date, you can use it anywhere a Date is needed. Here's some example code. The last java.util.Date output is just for verification:
code
import java.sql.*;
import java.util.*;

public class DateFromSQLDate
{

public DateFromSQLDate()
{
java.util.Date d = new java.util.Date();
java.sql.Date sd = new java.sql.Date( d.getTime() );

System.out.println( "d.getTime(): " + d.getTime() );
System.out.println( "d: " + d );
System.out.println( "sd.getTime(): " + sd.getTime() );
System.out.println( "sd: " + sd );

sd = sd.valueOf( sd.toString() );
System.out.println( "\nsd.getTime(): " + sd.getTime() );
System.out.println( "sd: " + sd );

d.setTime(sd.getTime());
System.out.println( "d.getTime(): " + d.getTime() );
System.out.println( "d: " + d );
} /* end constructor */

public static void main(String[] args)
{
new DateFromSQLDate();
}

} /* end class DateFromSQLDate */
[/code]

Lu Wei

Posts: 1
Registered: Oct 23, 2009 02:16:06 AM
Re: Date without time
Posted: Oct 23, 2009 02:22:24 AM   in response to: in response to: 's post
Click to report abuse...   Click to reply to this thread Reply
Or simply use regex:
Date start = new Date(System.currentTimeMillis());
String withoutTime = start.toString().replaceAll("(\\d\\d:){2}\\d\\d\\s", "");
System.out.print(withoutTime);
 Tags
Help

Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular type of content or application that you're viewing.

My tags shows your tags for this particular type of content or application that you're viewing.

 

MoreLess 


Point your RSS reader here for a feed of the latest messages in all forums