cancel
Showing results for 
Search instead for 
Did you mean: 

Problem saving a date in SQL server

Former Member
0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

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

Answers (0)