on 11-02-2005 5:28 PM
Hi,
I need to save the current day in my SQL Server (I need a type java.sql.Date).
The problem is that I need to save the date with the format "dd/MM/yyyy", otherwise I couldn't save it.
Is there any way of getting the current day of the type java.sql.Date (not the java.util.Date) with the format I need?
I have tried on several ways, but none of them works :-(. The last one I tried is:
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
java.sql.Date now = new java.sql.Date(new java.util.Date().getTime());
String dateNow = now.toString();
try{
java.sql.Date indate = new java.sql.Date(formatter.parse(dateNow).getTime());
}
catch(Exception e){}
Thanks in advance,
Mireia
Message was edited by: Mireia Romo
I don't think that java.sql.Date allows you to have a formatted date, you will have to update your SQL database with a string.
This code works pretty good for me.
package com.yorktowne;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main(String[] args) {
java.sql.Date now = new java.sql.Date(new java.util.Date().getTime());
String s = null;
s = format(now);
System.out.println(now);
System.out.println(s);
}
public static String format(Date im_date) {
// Convert Date to string and pass back
String str;
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy");
str = dateFormatter.format(im_date);
return str;
}
}
Regards,
Rich Heilman
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
85 | |
10 | |
10 | |
9 | |
6 | |
6 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.