How do I get a Date without time in Java?
Do you absolutely have to use java.util.Date
? I would thoroughly recommend that you use Joda Time or the java.time
package from Java 8 instead. In particular, while Date and Calendar always represent a particular instant in time, with no such concept as "just a date", Joda Time does have a type representing this (LocalDate
). Your code will be much clearer if you're able to use types which represent what you're actually trying to do.
There are many, many other reasons to use Joda Time or java.time
instead of the built-in java.util
types - they're generally far better APIs. You can always convert to/from a java.util.Date
at the boundaries of your own code if you need to, e.g. for database interaction.
How to get only date without time from calendar?
This might help
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 1);
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
String formatted = format1.format(cal.getTime());
System.out.println(formatted);
// Output "2020-10-19"
Removing time from a Date object?
The quick answer is :
No, you are not allowed to do that. Because that is what Date
use for.
From javadoc of Date
:
The class Date represents a specific instant in time, with millisecond precision.
However, since this class is simply a data object. It dose not care about how we describe it.
When we see a date 2012/01/01 12:05:10.321
, we can say it is 2012/01/01
, this is what you need.
There are many ways to do this.
Example 1 : by manipulating string
Input string : 2012/01/20 12:05:10.321
Desired output string : 2012/01/20
Since the yyyy/MM/dd are exactly what we need, we can simply manipulate the string to get the result.
String input = "2012/01/20 12:05:10.321";
String output = input.substring(0, 10); // Output : 2012/01/20
Example 2 : by SimpleDateFormat
Input string : 2012/01/20 12:05:10.321
Desired output string : 01/20/2012
In this case we want a different format.
String input = "2012/01/20 12:05:10.321";
DateFormat inputFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
Date date = inputFormatter.parse(input);
DateFormat outputFormatter = new SimpleDateFormat("MM/dd/yyyy");
String output = outputFormatter.format(date); // Output : 01/20/2012
For usage of SimpleDateFormat
, check SimpleDateFormat JavaDoc.
Get current date without time
You could use a Calendar
to solve this:
Calendar cal = Calendar.getInstance();
cal.setTime(yourDate);
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Date desiredDate = cal.getTime();
Java program to get the current date without timestamp
A java.util.Date
object is a kind of timestamp - it contains a number of milliseconds since January 1, 1970, 00:00:00 UTC. So you can't use a standard Date
object to contain just a day / month / year, without a time.
As far as I know, there's no really easy way to compare dates by only taking the date (and not the time) into account in the standard Java API. You can use class Calendar
and clear the hour, minutes, seconds and milliseconds:
Calendar cal = Calendar.getInstance();
cal.clear(Calendar.HOUR_OF_DAY);
cal.clear(Calendar.AM_PM);
cal.clear(Calendar.MINUTE);
cal.clear(Calendar.SECOND);
cal.clear(Calendar.MILLISECOND);
Do the same with another Calendar
object that contains the date that you want to compare it to, and use the after()
or before()
methods to do the comparison.
As explained into the Javadoc of java.util.Calendar.clear(int field):
The HOUR_OF_DAY, HOUR and AM_PM fields are handled independently and the the resolution rule for the time of day is applied. Clearing one of the fields doesn't reset the hour of day value of this Calendar. Use set(Calendar.HOUR_OF_DAY, 0) to reset the hour value.
edit - The answer above is from 2010; in Java 8, there is a new date and time API in the package java.time
which is much more powerful and useful than the old java.util.Date
and java.util.Calendar
classes. Use the new date and time classes instead of the old ones.
Java Date cut off time information
The recommended way to do date/time manipulation is to use a Calendar
object:
Calendar cal = Calendar.getInstance(); // locale-specific
cal.setTime(dateObject);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
long time = cal.getTimeInMillis();
Read date without timezone information
Assuming you are referring to the part of Florida following EST, you can set the timezone for SimpleDateFormat
and set your TimeZone to EST.
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
format.setTimeZone(TimeZone.getTimeZone("America/New_York"));
Date date = format.parse("2014-01-30 07:48:25");
Your parsed date now can be utilized by your default TimeZone
of the system (or set it to your liking as we did in the first place).
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
System.out.println(date);
The output I get for your date offset to UTC:
Thu Jan 30 12:48:25 UTC 2014
Related Topics
Java Date() Giving the Wrong Date
What's the Difference Between Getpath(), Getabsolutepath(), and Getcanonicalpath() in Java
What Is the List of Valid @Suppresswarnings Warning Names in Java
Using Mockito with Multiple Calls to the Same Method with the Same Arguments
Add Context Path to Spring Boot Application
Print an Integer in Binary Format in Java
Ignore Fields from Java Object Dynamically While Sending as JSON from Spring MVC
How to Implement a Db Listener in Java
@Runwith(Mockitojunitrunner.Class) VS Mockitoannotations.Initmocks(This)
What Is the Purpose of Flush() in Java Streams
Java - Get a List of All Classes Loaded in the Jvm
Open Resource with Relative Path in Java
Class Has Been Compiled by a More Recent Version of the Java Environment
Difference Between Validate(), Revalidate() and Invalidate() in Swing Gui
Field Required a Bean of Type That Could Not Be Found.' Error Spring Restful API Using Mongodb