How to convert sql date to java [duplicate] - java

This question already has answers here:
Using setDate in PreparedStatement
(6 answers)
Closed 6 years ago.
SO i have a table in my database, date declared as date. and now i'm coding in java and i need the DATE to be inputted by the user and store it in my database. how does it work? i mean to convert date in to what? thank you!!
for now this is my code
java.util.Date utilDate = new java.util.Date();
java.sql.Date sqldate = new java.sql.Date(utilDate.getTime());
System.out.print("更新日: ");
String 更新日 = input.nextLine();

If you are using SQL SERVER, the following code will convert your date to SQL Server format.
java.util.Date utilDate = new java.util.Date(); //date that needs conversion
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); //converts to SQL date format

Related

How to convert sql date format? [duplicate]

This question already has answers here:
display Java.util.Date in a specific format
(11 answers)
want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format
(11 answers)
Convert String date into java.util.Date in the dd/MM/yyyy format [duplicate]
(1 answer)
SimpleDateFormatter.parse giving output in different format than specified
(1 answer)
Android SimpleDateFormat, how to use it?
(11 answers)
Closed 2 years ago.
Hello I am trying to convert sql date format to "dd-MM-yyyy" but in the output I have this unwanted format which shows the current date time (Mon Aug 14 00.00....)
String inputDate = rs.getDate("BIRTH_DATE").toString();
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
Date outputDate = formatter.parse(inputDate);
user.setBirthdate(outputDate);
Dates do not have an inherent format.
java.util.Date inputDate = rs.getDate("BIRTH_DATE");
user.setBirthdate(inputDate);
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String representation = formatter.format(inputDate);
It would be better to use the new java.time classes: LocalDate, ZonedDateTime and so on.

Format java.sql.Date to dd-MMM-yy for setDate [duplicate]

This question already has answers here:
Best way to convert Java SQL Date from yyyy-MM-dd to dd MMMM yyyy format
(4 answers)
Closed 5 years ago.
Is there a way to change to format of java.sql.date from yyyy-mm-dd to dd-MMM-yy?
The code I have so
java.util.Date utilDate = new java.util.Date();
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
You can format java.sql.Date with java.text.SimpleDateFormat:
String strDate = new SimpleDateFormat("dd-MMM-yy").format(new java.sql.Date(0));

Insert Date in string format into a database having field type of Date

I am having a date in the following format as string.
String from_date = "2016-09-09";
I need to insert into into a database having date type field using PreparedStatement. I have done it in the following way.
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date fd = formatter.parse(from_date);
select1.setDate(1, fd);
But it is showing the error as
The method setDate(int.java.sql.Date) in the type PreparedStatement is not applicable to the arguments(int,java.util.Date)
You need to use a java.sql.Date when inserting in database. Try the following :
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date fd = formatter.parse(from_date);
java.sql.Date sqlDate = new java.sql.Date(fd.getTime());
select1.setDate(1, sqlDate);
Answered here: Type mismatch: cannot convert from java.util.Date to java.sql.Date
You are trying to use a method that accepts java.sql.Date instead of java.util.Date
Welcome to programming, mind the details
Using java.sql.Date
java.util.Date
Suppose you have a variable endDate of type java.util.Date, you make the conversion thus:
select1.setDate(1, new java.sql.Date(endDate.getTime());

How to insert java.util.Date object holding date and time into mysql database [duplicate]

This question already has answers here:
How do I set a full date & time sql using java, and not just the date?
(3 answers)
How to store Java Date to Mysql datetime with JPA
(13 answers)
Closed 8 years ago.
I am using following code to insert a java.util.Date object in MySQL. d1 is date object I'm getting from a function. I tried casting java.util.Date into java.sql.Date but this doesn't save the time part.
String sql = "INSERT INTO abc " +
"VALUES" + "('Zara',?)";
pstmt = conn.prepareStatement(sql);
pstmt.setDate(1, d1);
stmt.executeUpdate(sql);
Any Idea to store Date and time of Date Object in MySQL?
You need to convert java.util.Date instance to java.sql.Date format. and then use it with PreparedStatement.
Change:
pstmt.setDate( 1, d1 );
to:
pstmt.setDate( 1, new java.sql.Date( d1.getTime() );
If the field is of type datetime or timestamp, you have to use
pstmt.setTimestamp( 1, new java.sql.Timestamp( d1.getTime() );

converting java date to oracle date format [duplicate]

This question already has answers here:
Java: creating a date object from a string and inserting into MySQL
(3 answers)
Closed 9 years ago.
i am having string as 10-07-1992. i have to convert this to oracle date format like 10-jul-1992.
i tried using this
String date = fqu.getDob();
Date d = new SimpleDateFormat("dd/MM/yyyy").parse(date);
java.sql.Date sqlDate = new java.sql.Date(d.getTime());
System.out.println("sql date"+sqlDate);
this prints like 1992-07-10. but this is not recognized while inserting this value in oracle.
i dont need to_cast method. this works only in sql command prompt. i want to do this conversion inside java file.
Use Date format as dd-MMM-yyyy
Date d = new SimpleDateFormat("dd-MMM-yyyy").parse(date);
Shows output as
10-jul-1992
Can you try
Date d = new SimpleDateFormat("dd/MMM/yyyy").parse(date);

Categories

Resources