Parsing a string to date gives 01/01/0001 00:00:00 - java

String dateimput=request.getParameter("datepicker");
System.out.printl("datepicker:" +dateimput);
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
Date dt = null;
try
{
dt = df.parse(dateimput);
System.out.println("date imput is:" +dt);
} catch (ParseException e)
{
e.printStackTrace();
}
*datepicker:04/29/2010 (value I currently selected from datepicker).
*the field in database is typed date.
1-date imput is:Thu Apr 29 00:00:00 CEST 2010
and in database level it is inserted like that 01/01/0001 00:00:00

Your Java code will work fine.
04/29/2010 will give you a date object with the correct time/date set.
You said the problem is during the Database insert, so you should tell us the used database and post the code you are using for the insert.

Based on your comment to echox's answer. It looks like your problem might be not putting quotes around your date value in your insert statement.

Related

Date formatting issue for sql format 'YYYY-MM-DD HH24:MI:SS.FF' to java

In my sql query I have date formating as :
to_char(crtd_ts,'YYYY-MM-DD HH24:MI:SS.FF') crtd
and my database column stores the value as 2018-4-24.8.1. 30. 404577000
What is the way of doing the same thing in Java?
I tried this way, but I am getting an error.
private String formatDate(String refCrtdTs) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MMM-dd HH24:mm:ss.ff");
String dateInString = refCrtdTs;
try {
Date date = sdf.parse(dateInString);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
I am trying to replace the above sql query with hibernate criteria. My hibernate entity for the column is defined as
#Column(name="REF_CRTD_TS",columnDefinition="timestamp")
private String refCrtdTs;
and the column type in Oracle is Timestamp.
So hibernate criteria returns me this value as String which I want to format now.
Try this,
yy-M-d H:m:s.F
And your database column stores the value should be "2018-4-24 8:1:30.114"
F is Day in year (example: Feb 1st => F = 32, 31 day in Jan + 1 day in Feb)
You have to match same patter as per your sql, just try the same pattern to get in Java
I think this will work, please comment if you required more info
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS aa");

Output Mismatch while convert string value to date object

I have tried to convert string value to date object. I have tried some logic, but it didn't work. see my code :
public class Test {
public static void main(String[] args) {
try{
Test obj= new Test();
String date="2015-03-30T11:54:46.162430057Z";
Date dt=obj.getServerDate(date);
//System.out.println(dt);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public Date getServerDate(String str_date)
{
if (str_date == null)
return null;
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
System.out.println("Given date:" + str_date);
Date pars_date = null;
try
{
pars_date = fmt.parse(str_date);
System.out.println("Parsed date:" + pars_date);
}
catch (Exception e)
{
e.printStackTrace();
}
return pars_date;
}
}
The above code give the following output:
Given date:2015-03-30T11:54:46.162430057Z
Parsed date:Wed Apr 01 09:01:56 IST 2015
In the output the Given date and Parsed date is mismatch, I couldn't find any mistake in my code. Please let me know if you find the solution...
Thanks in advance.
The problem is with matching the server format with your parsing format.
You specify .SSS to handle milliseconds, but in the default configuration the number of "S" does not matter, SimpleDateFormat will consume until the next delimiter. This means that it will parse the first fields, and in the end add 162430057 milliseconds (about 45 hours) which gets you to the (correct, but unwanted and unexpected) Wed Apr 01 09:01:56 IST 2015.
If you are using Java 8 I recommend instead looking at DateTimeFormatter which can handle nanoseconds or if pre Java 8 use Joda Time.
If you are running on JDK 1.3 (which was End-Of-Life 2006/2007 depending on vendor) and you are certain of the time format, then you could use:
fmt(text.substring(0, 23) + 'Z')
or possibly remove the + 'Z'and remove the 'Z' from the format.
Using Date you will lose the nano second precision in any case as Date only holds milliseconds.

DateFormat does not work?

String selectedDate = "2012-" + createdMonth + "-" + createdDay;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
createdDate = dateFormat.parse(selectedDate);
} catch (ParseException e1) {
e1.printStackTrace();
}
System.out.println(createdDate);
Basically when I print createdDate, it will display something like this :
Thu Mar 08 00:00:00 CST 2012
Instead of something of this format "yyyy-MM-dd". Thanks a bunch!
The parse method returns a java.util.Date and that is the what the Date implementation of toString() returns.
You need to print as below. Point is that you need to use the formatter object you have created while printing as well.
System.out.println(dateFormat.format(createdDate));
use dateFormat.format(createdDate)
You seem to think that createdDate, which is a Date object, has the format yyyy-MM-dd. It doesn't. Date objects don't have a format - they just contain a timestamp, just like numbers are just numbers, which don't have a format by themselves.
A SimpleDateFormat object is used to parse a String into a Date object, or format a Date object into a String.
If you have a Date object and you want to display the date in a particular format, then convert it to a String with the appropriate format using a SimpleDateFormat object:
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
String text = fmt.format(createdDate);
System.out.println("Created: " + text);
If you print a Date object without explicitly formatting it, it will be formatted using a default format, which is why you see Thu Mar 08 00:00:00 CST 2012.
A Date object does not somehow remember what the format was of the String that you parsed it from.

Can't write time to database, only date. time gets ignored

I am trying to insert date and time but only the date goes through. when I check the database the time is always 00:00
public void suspendPart(String partId, String startDate,
String reasonCode, String endDate) throws DAOException {
System.out.println("original date: "+startDate); //output: original date: 04/01/2013 08:42
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm");
Map<String, Object> params = new HashMap<String, Object>();
params.put("p_id", Integer.parseInt(partId));
Date dateTest = DAOUtils.parseDate(startDate, format);
System.out.println("date after format " +dateTest); // output: date after format Fri Jan 04 08:42:00 GMT 2013
params.put("p_start_date", dateTest);
parts_package_add_part.execute(params);
}
I managed to make it work once but I don't know how. by the time I checked the database and saw a row with proper time, I had already messed with the code so it was lost.
Can anyone see what I'm doing wrong?
Edit: sp declaration
parts_package_add_part = SpringStoredProcedure
.getStoredProcedureCompiled(getJdbcTemplate(), false,
"parts_package.add_part",
new SqlParameter("part_id", OracleTypes.NUMBER),
new SqlParameter("p_start_date", OracleTypes.DATE));
Edit: I can't change the database. well not easily at least. I would need to get PLSQL developer to do it and that will take days
I recently had a simular problem. My solution was to use TIMESTAMP as oracle data type in the database table and the java.sql.Timestamp class in java. This matched fine...
Using Timestamp could help. What is the datatype used to store your date in db? May be it is a Date type. If you convert that to Timestame/DateTime it could help.
Hi try using timestamp datatype in your database table and also from your java code use java.sql.Timestamp it will store data and time both for you
Thanks

String to a HH:mm:ss time and finding the difference between 2 times

public static void stringToDate(String time, String time2) {
try {
DateFormat formatter = new SimpleDateFormat("HH:mm:ss");
Date t1 = formatter.parse(time);
System.out.println("Users first date: " + t1);
} catch (ParseException e){
System.out.println("Exception :" + e);
}
}
So above, I pass in 2 string parameters which are in the format of something like '17:23:56' and want them both converted into proper time objects that i can then find the difference between the 2, possibly in miliseconds or whatevers available if anyone knows how that'd be great.
Problem i'm having so far is that the output is: "Users first date: Thu Jan 01 17:23:56 GMT 1970", even though I thought I specified it to only parse it in HH:mm:ss. Anyone got the solution, thanks.
You're printing the result of Date#toString() ( <-- click the link!) which is indeed in the given format. If you want to present it in HH:mm:ss you have to use the format() method on the obtained Date.
System.out.println(formatter.format(t1));
Don't worry about this. Just parse the other time string to Date as well, do a getTime() on both and finally do the math.

Categories

Resources