java使用值字符串创建日期对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16208121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java create date object using a value string
提问by Marco Dinatsoli
I am using this to get the current time :
我正在使用它来获取当前时间:
java.util.Calendar cal = java.util.Calendar.getInstance();
System.out.println(new java.text.SimpleDateFormat("EEEE, dd/MM/yyyy/hh:mm:ss")
.format(cal.getTime()));
I want to put the value (which I print it) into a date object, I tried this:
我想将值(我打印它)放入一个日期对象中,我试过这个:
Date currentDate = new Date(value);
but eclipse tells me that this function is not good.
但是eclipse告诉我这个功能不好。
Editthe value
is the value that I printed to you using system.out.println
编辑的value
是,我打印您使用System.out.println的价值
采纳答案by Freak
Whenever you want to convert a String to Date object then use SimpleDateFormat#parse
Try to use
每当您想将 String 转换为 Date 对象时,SimpleDateFormat#parse
请使用Try to use
String dateInString = new java.text.SimpleDateFormat("EEEE, dd/MM/yyyy/hh:mm:ss")
.format(cal.getTime())
SimpleDateFormat formatter = new SimpleDateFormat("EEEE, dd/MM/yyyy/hh:mm:ss");
Date parsedDate = formatter.parse(dateInString);
.Additional thing is if you want to convert a Date
to String
then you should use SimpleDateFormat#format
function.
Now the Point for you is
new Date(String)
is deprecated and not recommended now.Now whenever anyone wants to parse , then he/she should use SimpleDateFormat#parse
.
.附加的事情是,如果你想将 a 转换为Date
,String
那么你应该使用SimpleDateFormat#format
函数。
现在,您的 Point 已new Date(String)
被弃用,现在不推荐使用SimpleDateFormat#parse
。现在,
无论何时有人想要解析 ,他/她都应该使用.
refer the official docfor more Date and Time Patterns used in SimpleDateFormat options.
有关SimpleDateFormat 选项中使用的更多日期和时间模式,请参阅官方文档。
回答by Suresh Atta
It is because value coming String (Java Date object constructor for getting string is deprecated)
这是因为值来自 String (用于获取字符串的 Java Date 对象构造函数已弃用)
and Date(String)
is deprecated.
并Date(String)
已弃用。
Have a look at jodatimeor you could put @SuppressWarnings({“deprecation”}) outside the method calling the Date(String)
constructor.
看看jodatime或者你可以把 @SuppressWarnings({“deprecation”}) 放在调用Date(String)
构造函数的方法之外。
回答by alexey28
Use SimpleDateFormat parse method:
使用 SimpleDateFormat 解析方法:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
String inputString = "11-11-2012";
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date inputDate = dateFormat.parse(inputString, dateFormat );
Since we have Java 8 with LocalDate I would suggest use next:
由于我们有带有 LocalDate 的 Java 8,我建议接下来使用:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
String inputString = "11-11-2012";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate inputDate = LocalDate.parse(inputString);
回答by Nikhil Agrawal
FIRST OF ALL KNOW THE REASON WHY ECLIPSE IS DOING SO.
首先要知道 ECLIPSE 这样做的原因。
Date has only one constructor Date(long date)which asks for date in long data type.
Date 只有一个构造函数Date(long date)要求长数据类型的日期。
The constructor you are using
您正在使用的构造函数
Date(String s)Deprecated. As of JDK version 1.1, replaced by DateFormat.parse(String s).
日期(字符串)已弃用。从 JDK 1.1 版开始,由 DateFormat.parse(String s) 取代。
Thats why eclipse tells that this function is not good.
这就是为什么eclipse告诉这个功能不好的原因。
See this official docs
看这个官方文档
http://docs.oracle.com/javase/6/docs/api/java/util/Date.html
http://docs.oracle.com/javase/6/docs/api/java/util/Date.html
Deprecated methods from your context -- Source -- http://www.coderanch.com/t/378728/java/java/Deprecated-methods
上下文中不推荐使用的方法 -- 来源 -- http://www.coderanch.com/t/378728/java/java/Deprecated-methods
There are a number of reasons why a method or class may become deprecated. An API may not be easily extensible without breaking backwards compatibility, and thus be superseded by a more powerful API (e.g., java.util.Date has been deprecated in favor of Calendar, or the Java 1.0 event model). It may also simply not work or produce incorrect results under certain circumstances (e.g., some of the java.io stream classes do not work properly with some encodings). Sometimes an API is just ill-conceived (SingleThreadModel in the servlet API), and gets replaced by nothing. And some of the early calls have been replaced by "Java Bean"-compatible methods (size by getSize, bounds by getBounds etc.)
不推荐使用方法或类的原因有很多。在不破坏向后兼容性的情况下,API 可能不容易扩展,因此会被更强大的 API 所取代(例如,java.util.Date 已被弃用,以支持 Calendar 或 Java 1.0 事件模型)。它也可能在某些情况下根本不起作用或产生不正确的结果(例如,某些 java.io 流类在某些编码下无法正常工作)。有时一个 API 只是构思不周(servlet API 中的 SingleThreadModel),并没有被任何东西取代。并且一些早期的调用已经被“Java Bean”兼容的方法所取代(getSize 的大小,getBounds 的边界等)
SEVRAL SOLUTIONS ARE THERE JUST GOOGLE IT--
有几个解决方案只是谷歌它--
You can use date(long date) By converting your date String into long milliseconds and stackoverflow has so many post for that purpose.
您可以使用 date(long date) 通过将日期字符串转换为长毫秒,并且 stackoverflow 有很多用于此目的的帖子。
回答by SudoRahul
What you're basically trying to do is this:-
您基本上要做的是:-
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
The reason being, the String
which you're printing is just a String
representation of the Date
in your required format. If you try to convert it to date, you'll eventually end up doing what I've mentioned above.
其原因是,在String
这你打印仅仅是一个String
的表示Date
你所需要的格式。如果您尝试将其转换为最新版本,您最终会执行我上面提到的操作。
Formatting Date
(cal.getTime()) to a String
and trying to get back a Date
from it - makes no sense. Date
has no format as such. You can only get a String
representation of that using the SDF.
将Date
( cal.getTime())格式化为 aString
并试图从中取回 a Date
- 毫无意义。Date
没有这样的格式。您只能String
使用 SDF获得表示。
回答by akki0996
import java.util.Date;
import java.text.SimpleDateFormat;
Above is the import method, below is the simple code for Date
上面是导入方法,下面是Date的简单代码
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
system.out.println((dateFormat.format(date)));
回答by JDGuide
Try this :-
尝试这个 :-
try{
String valuee="25/04/2013";
Date currentDate =new SimpleDateFormat("dd/MM/yyyy").parse(valuee);
System.out.println("Date is ::"+currentDate);
}catch(Exception e){
System.out.println("Error::"+e);
e.printStackTrace();
}
Output:-
输出:-
Date is ::Thu Apr 25 00:00:00 GMT+05:30 2013
Your value should be proper format.
您的值应该是正确的格式。
In your question also you have asked for this below :-
在您的问题中,您也在下面提出了这个要求:-
Date currentDate = new Date(value);
This style of date constructor is already deprecated.So, its no more use.Being we know that Date has 6 constructor.Read more
这种风格的日期构造函数已经被弃用了。所以,它不再使用了。因为我们知道日期有 6 个构造函数。阅读更多
回答by Hamzeen Hameem
Here is the optimized solution to do it with SimpleDateFormat
parse()
method.
这是使用SimpleDateFormat
parse()
方法完成的优化解决方案。
SimpleDateFormat formatter = new SimpleDateFormat(
"EEEE, dd/MM/yyyy/hh:mm:ss");
String strDate = formatter.format(new Date());
try {
Date pDate = formatter.parse(strDate);
} catch (ParseException e) { // note: parse method can throw ParseException
e.printStackTrace();
}
Few things to notice
需要注意的几点
- We don't need to create a Calendar instance to get the current date
& time instead use
new Date()
- Also it doesn't require 2 instancesof
SimpleDateFormat
as found in the most voted answer for this question. It's just a waste of memory - Furthermore, catching a generic exception like
Exception
is a bad practice when we know that theparse
method only stands a chance to throw aParseException
. We need to be as specific as possiblewhen dealing with Exceptions. You can refer, throws Exception bad practice?
- 我们不需要创建 Calendar 实例来获取当前日期和时间,而是使用
new Date()
- 此外,它不需要2个实例的
SimpleDateFormat
作为对这一问题的最投票的答案找到。这只是 浪费内存 - 此外,
Exception
当我们知道该parse
方法只有机会抛出一个ParseException
. 在处理异常时,我们需要尽可能具体。你可以参考,throws Exception 不好的做法吗?
回答by manasera
try this, it worked for me.
试试这个,它对我有用。
String inputString = "01-01-1900";
Date inputDate= null;
try {
inputDate = new SimpleDateFormat("dd-MM-yyyy").parse(inputString);
} catch (ParseException e) {
e.printStackTrace();
}
dp.getDatePicker().setMinDate(inputDate.getTime());