I want to remove time from Date
object.
我想从Date对象中删除时间。
DateFormat df;
String date;
df = new SimpleDateFormat("dd/MM/yyyy");
d = eventList.get(0).getStartDate(); // I'm getting the date using this method
date = df.format(d); // Converting date in "dd/MM/yyyy" format
But when I'm converting this date (which is in String
format) it is appending time also.
但是当我转换这个日期(它是字符串格式)时,它也是附加时间。
I don't want time at all. What I want is simply "21/03/2012".
我根本不需要时间。我想要的只是“21/03/2012”。
16 个解决方案
#1
40
The quick answer is :
快速的答案是:
No, you are not allowed to do that. Because that is what Date
use for.
不,你不能那样做。因为这就是日期的用途。
From javadoc of Date
:
自javadoc日起:
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.
但是,因为这个类只是一个数据对象。它不关心我们如何描述它。当我们看到2012/01/01 12:05:10.321的日期时,我们可以说是2012/01/01,这就是你需要的。有很多方法可以做到这一点。
Example 1 : by manipulating string
示例1:通过操作字符串
Input string : 2012/01/20 12:05:10.321
输入字符串:2012/01/20 12:05:10.321
Desired output string : 2012/01/20
期望输出字符串:2012/01/20。
Since the yyyy/MM/dd are exactly what we need, we can simply manipulate the string to get the result.
由于yyyy/MM/dd正是我们所需要的,我们可以简单地操作字符串以获得结果。
String input = "2012/01/20 12:05:10.321";
String output = input.substring(0, 10); // Output : 2012/01/20
Example 2 : by SimpleDateFormat
示例2:通过SimpleDateFormat。
Input string : 2012/01/20 12:05:10.321
输入字符串:2012/01/20 12:05:10.321
Desired output string : 01/20/2012
期望输出字符串: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.
要使用SimpleDateFormat,请检查SimpleDateFormat JavaDoc。
#2
93
You can remove the time part from java.util.Date by setting the hour, minute, second and millisecond values to zero.
您可以从java.util中删除时间部分。通过将小时、分钟、秒和毫秒值设置为零来确定日期。
import java.util.Calendar;
import java.util.Date;
public class DateUtil {
public static Date removeTime(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
}
#3
25
Apache Commons DateUtils has a "truncate" method that I just used to do this and I think it will meet your needs. It's really easy to use:
Apache Commons DateUtils有一个“truncatetable”方法,我刚刚使用这个方法,我认为它将满足您的需要。它真的很容易使用:
DateUtils.truncate(dateYouWantToTruncate, Calendar.DAY_OF_MONTH);
DateUtils also has a host of other cool utilities like "isSameDay()" and the like. Check it out it! It might make things easier for you.
DateUtils还有许多其他很酷的实用程序,如“isSameDay()”等。检查一下它!它可能会让你的事情变得更容易。
#4
11
What about this:
这个:
Date today = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
today = sdf.parse(sdf.format(today));
#5
6
What you want is impossible.
你想要的是不可能的。
A Date
object represents an "absolute" moment in time. You cannot "remove the time part" from it. When you print a Date
object directly with System.out.println(date)
, it will always be formatted in a default format that includes the time. There is nothing you can do to change that.
日期对象表示时间的“绝对”时刻。你不能“删除时间部分”。当您使用System.out.println(Date)直接打印日期对象时,它将始终以包含时间的默认格式进行格式化。你无法改变这一点。
Instead of somehow trying to use class Date
for something that it was not designed for, you should look for another solution. For example, use SimpleDateFormat
to format the date in whatever format you want.
您应该寻找另一种解决方案,而不是试图将类日期用于它不是为之设计的东西。例如,使用SimpleDateFormat以任何您想要的格式格式化日期。
The Java date and calendar APIs are unfortunately not the most well-designed classes of the standard Java API. There's a library called Joda-Time which has a much better and more powerful API.
不幸的是,Java日期和日历API并不是标准Java API中设计最好的类。有一个叫Joda-Time的库,它有更好更强大的API。
Joda-Time has a number of special classes to support dates, times, periods, durations, etc. If you want to work with just a date without a time, then Joda-Time's LocalDate
class would be what you'd use.
Joda-Time有许多特殊的类来支持日期、时间、周期、持续时间等。如果您想只使用一个没有时间的日期,那么Joda-Time的LocalDate类就是您要使用的。
#6
4
You can write that for example:
你可以这样写:
private Date TruncarFecha(Date fechaParametro) throws ParseException {
String fecha="";
DateFormat outputFormatter = new SimpleDateFormat("MM/dd/yyyy");
fecha =outputFormatter.format(fechaParametro);
return outputFormatter.parse(fecha);
}
#7
3
you could try something like this:
你可以试试这样的方法:
import java.text.*;
import java.util.*;
public class DtTime {
public static void main(String args[]) {
String s;
Format formatter;
Date date = new Date();
formatter = new SimpleDateFormat("dd/MM/yyyy");
s = formatter.format(date);
System.out.println(s);
}
}
This will give you output as21/03/2012
这将给你输出as21/03/2012
Or you could try this if you want the output as 21 Mar, 2012
或者你可以试试这个,如果你想要的输出是2012年3月21日
import java.text.*;
import java.util.*;
public class DtTime {
public static void main(String args[]) {
Date date=new Date();
String df=DateFormat.getDateInstance().format(date);
System.out.println(df);
}
}
#8
2
Date dateWithoutTime =
new Date(myDate.getYear(),myDate.getMonth(),myDate.getDate())
This is deprecated, but the fastest way to do it.
这是不赞成的,但是这是最快的方法。
#9
2
May be the below code may help people who are looking for zeroHour of the day :
下面的代码可能会帮助那些正在寻找零点的人:
Date todayDate = new Date();
GregorianCalendar todayDate_G = new GregorianCalendar();
gcd.setTime(currentDate);
int _Day = todayDate_GC.get(GregorianCalendar.DAY_OF_MONTH);
int _Month = todayDate_GC.get(GregorianCalendar.MONTH);
int _Year = todayDate_GC.get(GregorianCalendar.YEAR);
GregorianCalendar newDate = new GregorianCalendar(_Year,_Month,_Day,0,0,0);
zeroHourDate = newDate.getTime();
long zeroHourDateTime = newDate.getTimeInMillis();
Hope this will be helpful.
希望这能有所帮助。
#10
1
String substring(int startIndex, int endIndex)
In other words you know your string will be 10 characers long so you would do:
换句话说,你知道你的字符串有10个字符长,所以你会这样做:
FinalDate = date.substring(0,9);
#11
1
A bit of a fudge but you could use java.sql.Date. This only stored the date part and zero based time (midnight)
有点含糊,但是您可以使用java.sql.Date。这只存储了日期部分和基于0的时间(午夜)
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, 2011);
c.set(Calendar.MONTH, 11);
c.set(Calendar.DATE, 5);
java.sql.Date d = new java.sql.Date(c.getTimeInMillis());
System.out.println("date is " + d);
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
System.out.println("formatted date is " + df.format(d));
gives
给了
date is 2011-12-05
formatted date is 05/12/2011
Or it might be worth creating your own date object which just contains dates and not times. This could wrap java.util.Date and ignore the time parts of it.
或者,您可以创建自己的date对象,它只包含日期而不包含时间。这会使java.util状态。日期和忽略它的时间部分。
#12
1
java.util.Date represents a date/time down to milliseconds. You don't have an option but to include a time with it. You could try zeroing out the time, but then timezones and daylight savings will come into play--and that can screw things up down the line (e.g. 21/03/2012 0:00 GMT is 20/03/2012 PDT).
java.util。Date表示从毫秒到日期/时间。你没有别的选择,只能加上时间。你可以尝试调零时间,但是时区和夏令时将发挥作用——这可能会把事情弄糟(例如,21/03/2012 0:00 GMT是20/03/2012 PDT)。
What you might want is a java.sql.Date
to represent only the date portion (though internally it still uses ms).
您可能需要的是java.sql。日期只表示日期部分(尽管在内部它仍然使用ms)。
#13
1
In addtition to what @jseals has already said. I think the org.apache.commons.lang.time.DateUtils class is probably what you should be looking at.
在addtition中@jseals已经说过了。我认为org.apache.common .lang.time. dateutils类可能是您应该关注的。
It's method : truncate(Date date,int field) worked very well for me.
它的方法:截形(Date Date Date Date,int字段)对我来说非常有用。
JavaDocs : https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/time/DateUtils.html#truncate(java.util.Date, int)
JavaDocs:https://commons.apache.org/proper/commons lang/javadocs/api - 2.6 / - org/apache/commons/lang/time/dateutils.html #截断(java.util.Date int)
Since you needed to truncate all the time fields you can use :
由于需要截断所有可以使用的时间字段:
DateUtils.truncate(new Date(),Calendar.DAY_OF_MONTH)
#14
0
You can also manually change the time part of date and format in "dd/mm/yyyy" pattern according to your requirement.
您还可以根据您的要求,手动更改日期的时间部分,并在“dd/mm/yyyy”模式中设置格式。
public static Date getZeroTimeDate(Date changeDate){
Date returnDate=new Date(changeDate.getTime()-(24*60*60*1000));
return returnDate;
}
If the return value is not working then check for the context parameter in web.xml. eg.
如果返回值不工作,那么请检查web.xml中的上下文参数。如。
<context-param>
<param-name>javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE</param-name>
<param-value>true</param-value>
</context-param>
#15
0
Don't try to make it hard just follow a simple way
不要试图让它变得困难,只要遵循简单的方法
date is a string where your date is saved
date是保存日期的字符串
String s2=date.substring(0,date.length()-11);
now print the value of s2
. it will reduce your string length and you will get only date part.
现在打印s2的值。它将减少你的字符串长度,你将只得到日期部分。
#16
0
Another way to work out here is to use java.sql.Date as sql Date doesn't have time associated with it, whereas java.util.Date always have a timestamp. Whats catching point here is java.sql.Date extends java.util.Date, therefore java.util.Date variable can be a reference to java.sql.Date(without time) and to java.util.Date of course(with timestamp).
另一种解决方法是使用java.sql。日期作为sql Date没有与之关联的时间,而java.util。日期总是有一个时间戳。这里的关键点是java.sql。日期java.util延伸。目前为止,因此java.util。日期变量可以是对java.sql的引用。日期(没有时间)和java.util。当然日期(时间戳)。
#1
40
The quick answer is :
快速的答案是:
No, you are not allowed to do that. Because that is what Date
use for.
不,你不能那样做。因为这就是日期的用途。
From javadoc of Date
:
自javadoc日起:
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.
但是,因为这个类只是一个数据对象。它不关心我们如何描述它。当我们看到2012/01/01 12:05:10.321的日期时,我们可以说是2012/01/01,这就是你需要的。有很多方法可以做到这一点。
Example 1 : by manipulating string
示例1:通过操作字符串
Input string : 2012/01/20 12:05:10.321
输入字符串:2012/01/20 12:05:10.321
Desired output string : 2012/01/20
期望输出字符串:2012/01/20。
Since the yyyy/MM/dd are exactly what we need, we can simply manipulate the string to get the result.
由于yyyy/MM/dd正是我们所需要的,我们可以简单地操作字符串以获得结果。
String input = "2012/01/20 12:05:10.321";
String output = input.substring(0, 10); // Output : 2012/01/20
Example 2 : by SimpleDateFormat
示例2:通过SimpleDateFormat。
Input string : 2012/01/20 12:05:10.321
输入字符串:2012/01/20 12:05:10.321
Desired output string : 01/20/2012
期望输出字符串: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.
要使用SimpleDateFormat,请检查SimpleDateFormat JavaDoc。
#2
93
You can remove the time part from java.util.Date by setting the hour, minute, second and millisecond values to zero.
您可以从java.util中删除时间部分。通过将小时、分钟、秒和毫秒值设置为零来确定日期。
import java.util.Calendar;
import java.util.Date;
public class DateUtil {
public static Date removeTime(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
}
#3
25
Apache Commons DateUtils has a "truncate" method that I just used to do this and I think it will meet your needs. It's really easy to use:
Apache Commons DateUtils有一个“truncatetable”方法,我刚刚使用这个方法,我认为它将满足您的需要。它真的很容易使用:
DateUtils.truncate(dateYouWantToTruncate, Calendar.DAY_OF_MONTH);
DateUtils also has a host of other cool utilities like "isSameDay()" and the like. Check it out it! It might make things easier for you.
DateUtils还有许多其他很酷的实用程序,如“isSameDay()”等。检查一下它!它可能会让你的事情变得更容易。
#4
11
What about this:
这个:
Date today = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
today = sdf.parse(sdf.format(today));
#5
6
What you want is impossible.
你想要的是不可能的。
A Date
object represents an "absolute" moment in time. You cannot "remove the time part" from it. When you print a Date
object directly with System.out.println(date)
, it will always be formatted in a default format that includes the time. There is nothing you can do to change that.
日期对象表示时间的“绝对”时刻。你不能“删除时间部分”。当您使用System.out.println(Date)直接打印日期对象时,它将始终以包含时间的默认格式进行格式化。你无法改变这一点。
Instead of somehow trying to use class Date
for something that it was not designed for, you should look for another solution. For example, use SimpleDateFormat
to format the date in whatever format you want.
您应该寻找另一种解决方案,而不是试图将类日期用于它不是为之设计的东西。例如,使用SimpleDateFormat以任何您想要的格式格式化日期。
The Java date and calendar APIs are unfortunately not the most well-designed classes of the standard Java API. There's a library called Joda-Time which has a much better and more powerful API.
不幸的是,Java日期和日历API并不是标准Java API中设计最好的类。有一个叫Joda-Time的库,它有更好更强大的API。
Joda-Time has a number of special classes to support dates, times, periods, durations, etc. If you want to work with just a date without a time, then Joda-Time's LocalDate
class would be what you'd use.
Joda-Time有许多特殊的类来支持日期、时间、周期、持续时间等。如果您想只使用一个没有时间的日期,那么Joda-Time的LocalDate类就是您要使用的。
#6
4
You can write that for example:
你可以这样写:
private Date TruncarFecha(Date fechaParametro) throws ParseException {
String fecha="";
DateFormat outputFormatter = new SimpleDateFormat("MM/dd/yyyy");
fecha =outputFormatter.format(fechaParametro);
return outputFormatter.parse(fecha);
}
#7
3
you could try something like this:
你可以试试这样的方法:
import java.text.*;
import java.util.*;
public class DtTime {
public static void main(String args[]) {
String s;
Format formatter;
Date date = new Date();
formatter = new SimpleDateFormat("dd/MM/yyyy");
s = formatter.format(date);
System.out.println(s);
}
}
This will give you output as21/03/2012
这将给你输出as21/03/2012
Or you could try this if you want the output as 21 Mar, 2012
或者你可以试试这个,如果你想要的输出是2012年3月21日
import java.text.*;
import java.util.*;
public class DtTime {
public static void main(String args[]) {
Date date=new Date();
String df=DateFormat.getDateInstance().format(date);
System.out.println(df);
}
}
#8
2
Date dateWithoutTime =
new Date(myDate.getYear(),myDate.getMonth(),myDate.getDate())
This is deprecated, but the fastest way to do it.
这是不赞成的,但是这是最快的方法。
#9
2
May be the below code may help people who are looking for zeroHour of the day :
下面的代码可能会帮助那些正在寻找零点的人:
Date todayDate = new Date();
GregorianCalendar todayDate_G = new GregorianCalendar();
gcd.setTime(currentDate);
int _Day = todayDate_GC.get(GregorianCalendar.DAY_OF_MONTH);
int _Month = todayDate_GC.get(GregorianCalendar.MONTH);
int _Year = todayDate_GC.get(GregorianCalendar.YEAR);
GregorianCalendar newDate = new GregorianCalendar(_Year,_Month,_Day,0,0,0);
zeroHourDate = newDate.getTime();
long zeroHourDateTime = newDate.getTimeInMillis();
Hope this will be helpful.
希望这能有所帮助。
#10
1
String substring(int startIndex, int endIndex)
In other words you know your string will be 10 characers long so you would do:
换句话说,你知道你的字符串有10个字符长,所以你会这样做:
FinalDate = date.substring(0,9);
#11
1
A bit of a fudge but you could use java.sql.Date. This only stored the date part and zero based time (midnight)
有点含糊,但是您可以使用java.sql.Date。这只存储了日期部分和基于0的时间(午夜)
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, 2011);
c.set(Calendar.MONTH, 11);
c.set(Calendar.DATE, 5);
java.sql.Date d = new java.sql.Date(c.getTimeInMillis());
System.out.println("date is " + d);
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
System.out.println("formatted date is " + df.format(d));
gives
给了
date is 2011-12-05
formatted date is 05/12/2011
Or it might be worth creating your own date object which just contains dates and not times. This could wrap java.util.Date and ignore the time parts of it.
或者,您可以创建自己的date对象,它只包含日期而不包含时间。这会使java.util状态。日期和忽略它的时间部分。
#12
1
java.util.Date represents a date/time down to milliseconds. You don't have an option but to include a time with it. You could try zeroing out the time, but then timezones and daylight savings will come into play--and that can screw things up down the line (e.g. 21/03/2012 0:00 GMT is 20/03/2012 PDT).
java.util。Date表示从毫秒到日期/时间。你没有别的选择,只能加上时间。你可以尝试调零时间,但是时区和夏令时将发挥作用——这可能会把事情弄糟(例如,21/03/2012 0:00 GMT是20/03/2012 PDT)。
What you might want is a java.sql.Date
to represent only the date portion (though internally it still uses ms).
您可能需要的是java.sql。日期只表示日期部分(尽管在内部它仍然使用ms)。
#13
1
In addtition to what @jseals has already said. I think the org.apache.commons.lang.time.DateUtils class is probably what you should be looking at.
在addtition中@jseals已经说过了。我认为org.apache.common .lang.time. dateutils类可能是您应该关注的。
It's method : truncate(Date date,int field) worked very well for me.
它的方法:截形(Date Date Date Date,int字段)对我来说非常有用。
JavaDocs : https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/time/DateUtils.html#truncate(java.util.Date, int)
JavaDocs:https://commons.apache.org/proper/commons lang/javadocs/api - 2.6 / - org/apache/commons/lang/time/dateutils.html #截断(java.util.Date int)
Since you needed to truncate all the time fields you can use :
由于需要截断所有可以使用的时间字段:
DateUtils.truncate(new Date(),Calendar.DAY_OF_MONTH)
#14
0
You can also manually change the time part of date and format in "dd/mm/yyyy" pattern according to your requirement.
您还可以根据您的要求,手动更改日期的时间部分,并在“dd/mm/yyyy”模式中设置格式。
public static Date getZeroTimeDate(Date changeDate){
Date returnDate=new Date(changeDate.getTime()-(24*60*60*1000));
return returnDate;
}
If the return value is not working then check for the context parameter in web.xml. eg.
如果返回值不工作,那么请检查web.xml中的上下文参数。如。
<context-param>
<param-name>javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE</param-name>
<param-value>true</param-value>
</context-param>
#15
0
Don't try to make it hard just follow a simple way
不要试图让它变得困难,只要遵循简单的方法
date is a string where your date is saved
date是保存日期的字符串
String s2=date.substring(0,date.length()-11);
now print the value of s2
. it will reduce your string length and you will get only date part.
现在打印s2的值。它将减少你的字符串长度,你将只得到日期部分。
#16
0
Another way to work out here is to use java.sql.Date as sql Date doesn't have time associated with it, whereas java.util.Date always have a timestamp. Whats catching point here is java.sql.Date extends java.util.Date, therefore java.util.Date variable can be a reference to java.sql.Date(without time) and to java.util.Date of course(with timestamp).
另一种解决方法是使用java.sql。日期作为sql Date没有与之关联的时间,而java.util。日期总是有一个时间戳。这里的关键点是java.sql。日期java.util延伸。目前为止,因此java.util。日期变量可以是对java.sql的引用。日期(没有时间)和java.util。当然日期(时间戳)。