Question:
how to insert a date variable in java?
?
2017-01-14 03:15:02 UTC
i'm new to java. And I wanna know how to insert a date of birth using the Date variable in java. Is it really "Date" or not? if it is, how to use it? I still do not have an any idea of what "java,util.Date" and "java.sql.Date" are. I mean I simply don't know them. ):
I just want to insert someone's date of birth.

Thank you so much...! :D
Three answers:
david
2017-01-14 21:00:16 UTC
java.sql.Date is a SQL date, and there's no reason to use that unless you're storing the date in a database.



Some unfortunate things happened with the java.util.Date class. It was included in version 1 of Java, but it then became apparent that the class was not up to the task. There are things like time zones, daylight savings time, leap seconds, countries that use lunar calendars, and other things that make dates way more complicated than much of this class could handle. So although you can still use the java.util.Date class itself, many of its methods are deprecated. According to the documentation, "As of JDK 1.1, the Calendar class should be used to convert between dates and time fields and the DateFormat class should be used to format and parse date strings. The corresponding methods in Date are deprecated."



Although it says to use the Calendar class, you should probably actually use a subclass of it called the GregorianCalendar class, since this is the standard calendar that most of the western world uses. Similarly, you probably want to use the SimpleDateFormat subclass instead of the DateFormat class.



See source for a tutorial on Java dates.
George
2017-01-17 16:49:22 UTC
Insert where?

Do you mean you want to create a data variable with some value?



java.util.Date d = new java.util.Date();

That will create new date for you.



If you would like to set value then I suggest using Calendar object.. or you can even use DateFormat to parse date from String.

java.util.Date d = new Date();

java.text.DateFormat df = new SimpleDateFormat("MM/dd/yyyy");

d = df.parse("07/15/2010");





Using Calendar:

java.util.Calendar cal = Calendar.getInstance();

cal.set(Calendar.DATE, 15);

cal.set(Calendar.MONTH, Calendar.JULY);

cal.set(Calendar.YEAR, 2010);



java.util.Date calToDate = cal.getTime();
anonymous
2017-01-14 03:15:15 UTC
no


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...