My user interface returns a date as String (01/06/2016 2:30 am)to controller and I want to insert the into oracle 10 database by changing it from string to date and format to (dd-MMM-yy hh:mm:ss a) where the field is date type. Below is What I tried but getting Illegal Argument exception .
In controller I formatted to date and passed it to service layer through DTO
created : 01/06/2016 09:00 pm
SimpleDateFormat fromUser = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
SimpleDateFormat myFormat = new SimpleDateFormat("dd-MMM-yy hh:mm:ss a");
String reformattedStr = myFormat.format(fromUser.parse(created));
System.out.println("reformattedStr is : " + reformattedStr);
**reformattedStr 06-Jan-16 09:00:00 PM
Date formateDate=myFormat.parse(reformattedStr);
In service through prepared statement i am trying to insert , the date and other fields.
stmt.setTimestamp (8,new java.sql.Timestamp(news.getCreated().getTime()));
Can anyone please suggest?
Thanks for the help , I have updated the code, it might help some one.
If the column data type is timestamp, you don't need to do explicit conversion for the date. The statement.setTimestamp() method takes care of it.
There are two ways to create TimeStamp instance from a java.util.Date object.
new TimeStamp(date.getTime())
or
TimeStamp.value(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date))
The value returned by the news.getCreated().toString() method might not be returning the correctly formatted date - if it's returning formatted date.
Please refer to the javadoc for TimeStamp class for more information.
Thanks
Related
I am trying to print two dates using SimpleDateFormat but for my custom date the output looks completely different.
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date date2 =dateFormat.parse("01/01/2014 10:45:01");
System.out.println(("date2:"+date2));
Date date = new Date();
System.out.println(dateFormat.format(date)); // how it prints this is the desired outcome
OUTPUT:
date2:Wed Jan 01 10:45:01 GMT 2014
11/04/2014 10:45:50
The output is correct. You are creating date2 by parsing the date in string with the DateFormat. But when you print date2, you are not printing it with dateFormat.format() and hence the date is printed in its default format.
Try System.out.println("date2:"+dateFormat.format(date2));
format() will return you string of date in your desired format.
Date Object ---------->SDF Fomatter------>Formatted date in String
parse() accept the date in string format (your customize format) and return you Date Object
Formatted String date ------>SDF parse----->Date object
Wanna check, print it's value:
dateFormat.format(dateFormat.parse("01/01/2014 10:45:01"));
You have parsed it using dateformat, But you need to format it to get desired output.
System.out.println(("date2:"+dateFormat.format(date2)));
Did you try dateFormat.parse("dd/MM/yyyy HH:mm:ss"); ?
The line
System.out.println(("date2:"+date2));
Implicitly calls the toString() method on the date2 parameter. As Date has overridden the toString() method it inherits from Object, it is that method that dictates the format of the output. The Javadoc for Date#toString() states:
Converts this Date object to a String of the form:
dow mon dd hh:mm:ss zzz yyyy
Which matches what you're seeing in your output. In order to get the output that you were expecting, you would need to do the following instead:
System.out.println(("date2:" + dateFormat.format(date2)));
Date objects don't have a format associated with them. They're a dumb object that doesn't need to know about any display formatting details, as they aren't relevant to the date itself.
I am storing my 2 Java date types as Date and Time for a MySQL database table. I am using the SimepleDateFormat("YYYY-MM-dd") to store the date in my database and it shows up as the correct date when i go to select it. However when i try to parse it back into a util.Date and create a new Event Object, it shows up as 30/12/2012 instead of 31/05/2013 as it is in the database. The time, when parsed into a util.Date and formatted prints out correctly. I am not sure why the Date is printing the wrong date, but the time is printing the correct time.
Database
+--------+--------------+-----------+
+ EVENT1 + 2013-05-31 + 02:30:00 +
+--------+--------------+-----------+
+ EVENT2 + 2013-05-31 + 01:00:00 +
+--------+--------------+-----------+
Prints:
Event1
30/12/2012
02:30
Event2
30/12/2012
01:00
It should be yyyy-MM-dd with lower case Ys. See here for what the capital Y means...
Y returns 2012 while y returns 2011 in SimpleDateFormat
Your pattern is wrong. (mm != MM, yyyy != YYYY ...)
Take a look at http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
try
String testDate = "2007-11-02T14:46:03";
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date date = formatter.parse(testDate);
But better way to store in database is to use timestamp instead of storing date and time separately
The proper method is rs.getDate(int). Take a look at
http://docs.oracle.com/javase/6/docs/api/java/sql/ResultSet.html#getDate(int)
With that method you get a java.sql.Date and if you want to transform it to a java.util.Date take a look at this Converting java.sql.Date to java.util.Date
You can even do this
Date date = rs.getTimestamp(2);
By the way, is better to have your date object independent on the format you want to use to show it.
try this...
SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd");
Date date = dateFormat.parse(rs.getDate(2).toString());
I have a table in MySQL server called Caller_List. In this table I have a single date column called call_date whose data type is Date. I have created a web page in which I have a SELECT Box for from_day (where all days 1 to 31 are stored), a SELECT box for month (where all month names from January to December are stored), a SELECT box for year (where all years from 2000 to 2012 are stored. Like I also have a SELECT box for to_day, to_month and to_year.
The problem is when I fetch these day,month and year from java servlet using request.getParameter() method, it is fetched as string data type and stored in variable called from_date and to_date. I concatenate from_day,from_month and from_year and store in the variable called from_date. Also I concatenate to_day,to_month and to_year and store in to_date. I concatenate them in the format year-month-day since MySQL understands this format.
I then pass the following query to retrieve data between these two from_date and two date:
select caller_name,call_date
from Caller_List
where call_date>='"+from_date+"' and call_date<='"+to_date+"'
I also tried the following query but in vain:
select caller_name,call_date
from Caller_List
where call_date between '"+from_date+"' and '"+to_date+"'
I also came to know that I need to convert from_date and to_date to date format before executing the query. But I am a novice in java, I do not know how to do it. Also I want the date in the format year-month-date. I do want to display time with date, please please help me!
If you only want to know how to parse a String to a Date in java, you could simplely use the SimpleDateFormat class.
final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
final Date fromDate = dateFormat.parse(from_date);
} catch (ParseException e) {
// ...
}
Assume that your from_date string is looks like 2012-09-16.
And if you want to display a date with time. You can also use the SimpleDateFormat.
final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try {
final String displayDate = dateFormat.format(new Date());
} catch (ParseException e) {
// ...
}
The displayDate should looks like 2012-09-16 20:13:25.
One more thing, you could compare a string type and a date type in MySQL. Just make sure your date string has the pattern "yyyy-mm-dd hh:mm:ss". Of course, you should use the preparedStatement.
I want to convert the string time to Timestamp Object
My code for parsing is like this
String ts = "120918 10:35:45";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMdd hh:mm:ss");
java.util.Date parsedDate = dateFormat.parse(ts);
//parsing timestamp
Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());
System.out.println("timestamp after parsing :: "+timestamp);
It gives me result :-- timestamp after parsing :: 2012-09-18 10:35:45.0
But I do not want milliseconds part. I want only this -- 2012-09-18 10:35:45
Please help me in removing milliseconds part.
Timestamp is a container of milliseconds. The toString() is formatting it's contains based on what it thinks is best to be displayed.
If you want to format the value, you should use a date formatter and not use the value returned by the Timestamp object.
SimpleDateFormat noMilliSecondsFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(noMilliSecondsFormatter.format(timestamp));
nb. The value you have (after you've converted it) does not contain any milliseconds anyway...
My guess, probably you didn't use "noMilliSecondsFormatter" in println().
The method java.util.Date.getTime() according to its javaDoc:
Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT
represented by this Date object
Therefore, when you create the TimeStamp object, you are already passing mileseconds.
I have My Database data in this format
18-NOV-10
I have to pass the same format into java.util.Date like this
Date date = new java.util.Date(dateformater);
so that the result of java.util.Date is like this 18-NOV-10
Is this possible ??
I tried this way
String strDate = "12-NOV-07";
SimpleDateFormat sdfSource = new SimpleDateFormat("dd-MMM-yy");
Date date = sdfSource.parse(strDate);
System.out.println(date);
But i am getting the result as "Mon Nov 12 00:00:00 IST 2007 " which i want it only
12-NOV-07"
You can use java.text.DateFormat (actually SimpleDateFormat) to get you where you want to go, but maybe you shouldn't be storing the dates as strings in your database. It will do output and parsing.
SimpleDateFormat sdf =
new SimpleDateFormat("DD-MMM-YY");
Date parsed = sdf.parse(dateString);
See http://javatechniques.com/blog/dateformat-and-simpledateformat-examples/
Once you get the Date, you can turn it into the format you want but it will be held in memory as a Date object. You can get it in the form you want using
String dateString = sdf.format(parsed);
As others have pointed out, you should probably store your dates as dates, not strings; nevertheless...
If you want to turn a Date back into a string in that format you can use the following:
DateFormat formatter = new SimpleDateFormat("dd-MMM-yy");
Date date = new Date();
String dateStr = formatter.format(date); // Gives "22-May-11"
If you need MAY instead of May, just use toUpperCase() on the resultant string.
DateFormat sdf = new SimpleDateFormat("dd-MMM-yy");
Date d = sdf.parse("18-NOV-10");
Try System.out.println(sdfSource.format(date).toUpperCase()); instead. The Date object will always have a time component to it; there is no way to "disable" that feature. What you can do instead is to ignore it in your calculations and display. If all Date objects you use are set to the same time of the day, then you can safely ignore the effect of the time component in your comparisons. If you look carefully, the time component of your Date object is set to midnight.