I need to parse this date to get results from database, i tried to make it by many ways, but I can't find any fine. As i read it should work.
//2016-01-04
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSS");
Timestamp filterDateFromTs = new Timestamp ((dateFormat.parse(filterDateFrom)).getTime());
But I get
java.text.ParseException: Unparseable date: "2016-01-04".
You are trying to parse 2016-01-04 hence you should be using yyyy-MM-dd and not yyyy-MM-dd hh:mm:ss:SSS.
Try this instead:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Timestamp filterDateFromTs = new Timestamp ((dateFormat.parse(filterDateFrom)).getTime());
Related
When I get the date and time in MySQL it retrieves it in this format:
2016-01-14 14:24:00.0
Where does the .0 come from and how do I get this format with Java:
2016-01-14 14:24:00
You can use 2 ways to do this.
Split the date string at '.'
String date = "2016-01-14 14:24:00.0";
String newDate = date.split("\\.")[0];
System.out.println(newDate);
Use SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date date = sdf.parse("2016-01-14 14:24:00.0");
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(date));
If you are getting date as string, you can use Format to get the format you need:
SELECT DATE_FORMAT(YourDateField, '%d-%m-%Y %T')
FROM YourTable
Have a look here for all possible formats
If you are getting your date as a java.sql.Timestamp, you can get the corresponding java.util.Date instance very easily and then format it to the desired string representation with a java.text.SimpleDateFormat class.
I used a JXDATEPICKER in a JFrame. But, the date appears in the following form:
ven.15/10/2014
I want the date appears in the following format: yyyy-mm-dd
thanks.
You can use SimpleDateFormat like this:
SimpleDateFormat sf= new SimpleDateFormat( "yyyy-mm-dd" );
res = sf.format(myDate);
You can use the SimpleDateFormat to format the date in any desired format. Get the standard Date object from JXDatePicker.getDate() method, then use SimpleDateFormat to get the date in yyyy-mm-dd format.
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
Date dateFrom = JXDatePicker.getDate();
String resultDateFrom = formatter.format(dateFrom);
if(sqlData.get(i) != null){
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
java.sql.Timestamp myDate = new java.sql.Timestamp(dateFormat.parse(sqlData.get(i)).getTime());
preparedStatement.setTimestamp(i+1, myDate);
I'm not sure what i'm doign wrong and i keep geting "java.text.ParseException: Unparseable date:" on the 3rd line.
These are dates that i have pulled out of a database, and are set in the past.
System.out.println(sqlData.get(i));
prints out : 2014-04-19 05:48:22
java.text.ParseException: Unparseable date
is thrown from dateFormat.parse( when the string passed to it is not in the same format as the specified pattern.
Also note that you have not posted the entire exception message (the entire line). It would have been
java.text.ParseException: Unparseable date:2014-04-19 05:48:22
In your case
2014-04-19 05:48:22 is not in the format "yyyy/MM/dd HH:mm:ss"
Your pattern string should be
yyyy-MM-dd HH:mm:ss Note the - instead of /
Here's an example of parsing a date and then creating a timestamp out of it. This was testing and is working, so if you run into any errors, it may have something to do with the data you have and not the code itself.
String dateString = "1999/10/24 10:24:99";
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date dateObj = dateFormat.parse(dateString);
Timestamp timeStamp = new Timestamp(dateObj.getTime());
I have to get time from entire date
e.g. time=11:00:00 from date 2012-09-01 11:00:00.0
I tried following snippet but getting error Error : Unparseable date: "2012-9-1.13.30. 0. 0"
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = inputFormat.parse(iResultSet1.getString(i));
DateFormat outputFormat = new SimpleDateFormat("hh:mm:ss");
String outputString = outputFormat.format(date);
Edit: Now I am getting only date instead I want only time
if (iResultSet1.getDate(i) != null) {
Date date = iResultSet1.getDate(i);
System.out.println("date-->" + date);
// Format date into output format
DateFormat outputFormat = new SimpleDateFormat(
"HH:mm:ss");
String outputString = outputFormat.format(date);
// System.out.println("date1-->"+date1);
I wil suggest, in this case rather doing parsing and manipulation in java change your SQL to format and return only date as
Example
SELECT TIME_FORMAT(NOW(), '%H:%i:%s');
You are probably retrieving your date from the database (iResultSet1.getString(i)) and the problem is that you're getting wrong format, i.e. 2012-9-1.13.30. 0. 0. Either change the date format in the database or use:
Date date = iResultSet1.getDate(i);
instead of
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = inputFormat.parse(iResultSet1.getString(i));
forum member
I am having one problem with date time in java. Actually I am receiving the startdate in format 2012-02-27T01:10:10 and I want to insert the received date to my database having datetime datatype.
Actually I tried to convert the startdate received to datetime by below code
String sDate = jsonObject.get("StartDate").toString();
String eDate = jsonObject.get("EndDate").toString();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date startD = sdf.format(sDate);
Date endD = sdf.format(eDate);
but with the above code only date gets added to my database like 2012-02-27 00:00:00
I want to add the time also to my database but when I change the SimpleDateFormat to SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); nothing works.
please suggest me some solution I can apply so my time also gets added to database. I am using Hibernate JPA as my persistence layer.
SimpleDateFormat's format() method doesn't return a Date type.
try this:
Date startDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(sDate);
Try this,
yyyy-MM-dd'T'HH:mm:ss
String sDate = jsonObject.get("StartDate").toString();
String eDate = jsonObject.get("EndDate").toString();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date startD = sdf.format(sDate);
Timestamp startTime = new Timestamp(startD.getTime());
Date endD = sdf.format(eDate);
Timestamp endTime = new Timestamp(endD.getTime());
Of course only the date is parsed, since the pattern you provided to the SimpleDateFormat constructor only contains the date part! Add the time part to it and it will parse the time too just fine.
you can try like this....
DateFormat format = new SimpleDateFormat("MMddyyHHmmss");
Date date = format.parse("022310141505");