[英]Typecast sql.date to calendar
I have some code that uses the sql.date object to get the current date in the format: yyyy-MM-dd. 我有一些代码使用sql.date对象以yyyy-MM-dd格式获取当前日期。 I want to typecast that to a Calendar object that has the same format as the sql.date.
我想将其类型转换为与sql.date格式相同的Calendar对象。
Here is the code for the sql.date: 这是sql.date的代码:
java.util.Date now = new java.util.Date();
java.sql.Date sqlDate = new java.sql.Date( now.getTime() );
Edit: 编辑:
I want to know how to put this in a Calendar object. 我想知道如何将其放在Calendar对象中。 Is it even possible to do this?
甚至有可能这样做吗?
A java.sql.Date
doesn't have a format. 一个
java.sql.Date
不具备的格式。 It's just a date. 这只是个约会。
To convert it to a string, use a SimpleDateFormatter
with the relevant format set - or just use toString
if you definitely want yyyy-MM-dd
. 要将其转换为字符串,请使用具有相关格式集的
SimpleDateFormatter
或如果确实需要yyyy-MM-dd
则只需使用toString
。 (Unfortunately it's unclear which time zone you should use - java.sql.Date
is very poorly documented in this respect. I suspect it will use the default system time zone.) (不幸的是,目前尚不清楚应使用哪个时区-在这方面
java.sql.Date
文档非常少。我怀疑它将使用默认的系统时区。)
To create a Calendar
object with the given date, you can just use: 要创建具有给定日期的
Calendar
对象,您可以使用:
Calendar calendar = Calendar.getInstance();
calendar.setTime(sqlDate);
Again, a Calendar
doesn't have a text format either - it represents a point in time in a particular calendar system in a particular time zone, but nothing about a textual representation. 同样,
Calendar
也不具有文本格式-它表示特定日历系统在特定时区的某个时间点,但是与文本表示无关。
Java Date
objects (and note that java.sql.Date
is a java.util.Date
) don't have "formats". Java
Date
对象(请注意, java.sql.Date
是 java.util.Date
)没有“格式”。 They represent an instant in time. 它们代表时刻。
Calendar
also doesn't have a "format". Calendar
也没有“格式”。
What you're asking for doesn't make sense in java. 您要的内容在Java中没有意义。
Consider using SimpleDateFormat
to format one of these things into a String: 考虑使用
SimpleDateFormat
将其中之一格式化为String:
String str = new SimpleDateFormat("yyyy-MM-dd").format(myDate);
or if you use a Calendar
object (not recommended): 或者,如果您使用
Calendar
对象(不建议使用):
String str = new SimpleDateFormat("yyyy-MM-dd").format(myCalendar.getTime());
Use SimpleDateFormat#format() 使用SimpleDateFormat#format()
System.out.println(new SimpleDateFormat("yyyy-MM-dd")
.format(Calendar.getInstance().getTime()));
You can use the following snippet. 您可以使用以下代码段。
public class DateTest {
public static void main(String[] arg){
Calendar calendar = Calendar.getInstance();
//setting a valid date
Date date = new Date(113,07,10);
//prints the date as per format
System.out.println("Date in YYYY-MM-DD : " +date);
//sets the calendar with the date value in java.sql.Date instance
calendar.setTime(date);
//use simple date formatter to format the value in calendar instance
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("Date formatted from Calendar instance : "+dateFormat.format(calendar.getTime()));
}
} }
Please let me know what happens! 请让我知道会发生什么!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.