You can do it the same way as a java.util.Date (since java.sql.Date is a sub-class of java.util.Date) with a SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat(
"MM-dd-yyyy");
int year = 2014;
int month = 10;
int day = 31;
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1); // <-- months start
// at 0.
cal.set(Calendar.DAY_OF_MONTH, day);
java.sql.Date date = new java.sql.Date(cal.getTimeInMillis());
System.out.println(sdf.format(date));
Output is the expected
Answer from Elliott Frisch on Stack Overflow10-31-2014
You can do it the same way as a java.util.Date (since java.sql.Date is a sub-class of java.util.Date) with a SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat(
"MM-dd-yyyy");
int year = 2014;
int month = 10;
int day = 31;
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1); // <-- months start
// at 0.
cal.set(Calendar.DAY_OF_MONTH, day);
java.sql.Date date = new java.sql.Date(cal.getTimeInMillis());
System.out.println(sdf.format(date));
Output is the expected
10-31-2014
Use below code i have convert today date. learn from it and try with your code
Date today = new Date();
//If you print Date, you will get un formatted output
System.out.println("Today is : " + today);
//formatting date in Java using SimpleDateFormat
SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("MM-dd-yyyy");
String date = DATE_FORMAT.format(today);
System.out.println("Today in MM-dd-yyyy format : " + date);
Date date1 = formatter.parse(date);
System.out.println(date1);
System.out.println(formatter.format(date1));
datetime - How to format a java.sql Timestamp for displaying? - Stack Overflow
jsp - how can i format the java.sql.Date to MM/dd/yyyy - Stack Overflow
java.sql.Date formatting - Stack Overflow
How do I parse a String into java.sql.Date format?
Can't your SQL server accept strings for dates? The ones I've used can, so I just do something like:
String sqlDate = new SimpleDateFormat("yyyy-MM-dd").format(new SimpleDateFormat("dd-MM-yyyy").parse(startDate));... and pass sqlDate to the parametrized query. Like konrad mentioned, lowercase 'mm' is for minutes, and uppercase 'MM' is for month, so I think that's where your problem was.
More on reddit.comVideos
java.sql.Timestamp extends java.util.Date. You can do:
String s = new SimpleDateFormat("MM/dd/yyyy").format(myTimestamp);
Or to also include time:
String s = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(myTimestamp);
Use String.format (or java.util.Formatter):
Timestamp timestamp = ...
String.format("%1
TT", timestamp)
EDIT:
please see the documentation of Formatter to know what TD and TT means: click on java.util.Formatter
The first 'T' stands for:
't', 'T' date/time Prefix for date and time conversion characters.
and the character following that 'T':
'T' Time formatted for the 24-hour clock as "%tH:%tM:%tS".
'D' Date formatted as "%tm/%td/%ty".
Please don't do like that . Get the date Using Date Object and parse it Using SimpleDateFormat . its Simple . Don't Go For Hard Coding the value. its Not Convenient for programmer . please find Code
Date date = new Date();//Today Date
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");// FOrmat in This Format or you change Change as well
String Format= format.format(date);
System.out.println(Format);// 01/06/2016
or
Calendar TodayDate = Calendar.getInstance();
TodayDate.add(Calendar.DAY_ON_MONTH, -1);//import java.util.*
Date yesterday = cal.getTime();
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
String Format = format.format(yesterday);
System.out.println(Format);// 01/06/2016
To get yesterdays date and format it, you have 3 choices:
// Use Java 8+ LocalDate
java.time.LocalDate yesterday = LocalDate.now().minusDays(1);
String text = yesterday.format(DateTimeFormatter.ofPattern("MM/dd/uuuu"));
// Use Joda-Time (3rd-party library)
org.joda.time.LocalDate yesterday = LocalDate.now().minusDays(1);
String text = yesterday.toString("MM/dd/yyyy");
// Use Calendar (all Java versions, no 3rd-party library)
java.util.Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_ON_MONTH, -1);
java.util.Date yesterday = cal.getTime();
String text = new SimpleDateFormat("MM/dd/yyyy").format(yesterday);
Note that Calendar version still produces a "date" value with time-of-day. Extra code would be needed to clear the time fields.
java.sql.Date extends java.util.Date so it has the same information but displays it differently because of overridden toString() method.
If you do something like this you will see it is the same
System.out.println(new java.util.Date(sqlDate.getTime()));
It is advised to use DateFormat or SimpleDateFormat to display data, see the comments.
Use time stamp
Date updated_date=new Date();
Timestamp timestamp = new Timestamp(updated_date.getTime());
updated_date = timestamp;
and make sure Data type in Database should be Timestamp or Datetime
Alright, so I've got a file that I am reading and importing dates into a database using SQL. In order to do this, I need to parse the date from the file into a java.sql.Date. Here is an example date.
01-16-2014
And I need it in a format like
2014-01-16
I tried this.
String startDate="01-06-2014";
SimpleDateFormat sdf1 = new SimpleDateFormat("dd-mm-yyyy");
java.util.Date date = sdf1.parse(startDate);
java.sql.Date sqlStartDate = new java.sql.Date(date.getTime());But I got an error saying that I couldn't parse a java.util.Date to a java.sql.Date through a method invocation. Is there an easy way to do this?
Thanks in advance for your help!
Can't your SQL server accept strings for dates? The ones I've used can, so I just do something like:
String sqlDate = new SimpleDateFormat("yyyy-MM-dd").format(new SimpleDateFormat("dd-MM-yyyy").parse(startDate));
... and pass sqlDate to the parametrized query. Like konrad mentioned, lowercase 'mm' is for minutes, and uppercase 'MM' is for month, so I think that's where your problem was.
I would use the hacky way (assuming all your numbers are 2 or 4 digit) :)
String s = "01-16-2014";
java.sql.Date d = java.sql.Date.valueOf(s.substring(6) + "-" + s.substring(0, 5));
BTW, "mm" is for minutes
A java.util.Date is not a java.sql.Date. It's the other way around. A java.sql.Date is a java.util.Date.
You'll need to convert it to a java.sql.Date by using the constructor that takes a long that a java.util.Date can supply.
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
These are all too long.
Just use:
new Date(System.currentTimeMillis())