I Have a bean , in that I have one property of date type.
private Date insurance_date;
public Date getInsurance_date() {
return insurance_date;
}
public void setInsurance_date(Date insurance_date) {
this.insurance_date = insurance_date;
}
But get method gives us a date with time so I wrote a formated method as,
public String getFormatedDoI() {
DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
String stirngDate = null ;
if(insurance_date != null)
stirngDate = df.format((insurance_date));
return stirngDate;
}
But the problem is I have date in db as 2014-05-21 , when I use getFormatedDoI() method it prints 21-00-2014. In fact for all the dates in place of month it displays 0. How can I get exact formated date.? and the database I am using is mysql. The date coming from MYSQL database.
Small mm is for minutes, capital MM is for month number, if you want to get month name you can use MMM, in your case use "dd-MM-yyy" instead of 'dd-mm-yyyy'.
public String getFormatedDoI() {
DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
String stirngDate = null ;
if(insurance_date != null)
stirngDate = df.format((insurance_date));
return stirngDate;
}
Should solve your problem :)
mm takes minute.
If you want month then use MM.
so instead of DateFormat df = new SimpleDateFormat("dd-mm-yyyy");
use : DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
See this documentation on date format.
try
DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
instead of
DateFormat df = new SimpleDateFormat("dd-mm-yyyy");
It should resolve
Use "dd-MM-yyyy" instead of "dd-mm-yyyy". The pattern "mm" stands for minutes not for month.
Related
Having trouble in the following code. The output is dateStr: 11-Jan-11. Can anyone tell me why the date is modified?
String dateStr="";
String actionCompletionDueDate = "16/11/2011";
DateFormat srcDf = new SimpleDateFormat("mm/dd/yyyy");
DateFormat destDf = new SimpleDateFormat("dd-MMM-yy");
if(actionCompletionDueDate != null && !actionCompletionDueDate.equals("")) {
// parse the date string into Date object
System.out.println("actionCompletionDueDate: " + actionCompletionDueDate);
Date actionCompletionDate = srcDf.parse(actionCompletionDueDate);
dateStr = destDf.format(actionCompletionDate);
System.out.println("dateStr: " + dateStr);
}
Change
DateFormat srcDf = new SimpleDateFormat("mm/dd/yyyy");
to
DateFormat srcDf = new SimpleDateFormat("dd/MM/yyyy");
OR pass a correct string (which respects your format) to your code
String actionCompletionDueDate = "11/16/2011";
and correct the format to DateFormat srcDf = new SimpleDateFormat("MM/dd/yyyy");
mm is for minutes and MM is for months
String actionCompletionDueDate = "16/11/2011";
Should be
String actionCompletionDueDate = "11/16/2011";
Change
DateFormat srcDf = new SimpleDateFormat("mm/dd/yyyy");
to
DateFormat srcDf = new SimpleDateFormat("MM/dd/yyyy");
small mm here corresponds to minute.
But if you print source date using,
System.out.println(actionCompletionDate.toString());
Output is :
Sun Jan 16 00:11:00 IST 2011
See, 11 minute in time.
And change source date too, to 11/16/2011.
I'm trying to parse the following string to a Date object:
String str = "04/15/2014 10:30:24"
I'm using SimpleDateFormat :
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
java.util.Date orderDate = sdf.parse(str);
java.sql.Date orderSqlDate = new java.sql.Date(orderDate.getTime());
but orderSqlDate always returned: 04/15/2014 00:00:00
how to use SimpleDateFormat in java exactly?
The java.sql.Date javadoc states
To conform with the definition of SQL DATE, the millisecond values
wrapped by a java.sql.Date instance must be 'normalized' by setting
the hours, minutes, seconds, and milliseconds to zero in the
particular time zone with which the instance is associated.
If you're going to use java.sql.Date, there's no way around this.
You are also doing correct.
But to get the result in the format you want, you need to use .format("/your format/") method after parsing the string.
String date = "15/12/2014 10:42:24";
SimpleDateFormat dateParser = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date parseDate = dateParser.parse(date);
formatter.format(parseDate) // this will change format of date as you want.
I don't think the way you parse is wrong. Are you sure you print orderDate right ?
The following code demonstrates both parsing and formatting (printing).
public static void main(String[] args) {
String format = "MM/dd/yyyy HH:mm:ss";
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date orderDate = new SimpleDateFormat(format).parse("04/15/2014 10:30:24");
System.out.println(sdf.format(orderDate));
} catch (Exception e) {
e.printStackTrace();
}
}
Provide Locale in the SimpleDateFormat constructor, otherwise parsing might be dependant on your local settings:
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss", Locale.ROOT);
I want to converting the String (returns string :odb.getCloseDate()) into Date in the below code. But I am not getting the output in this formate "23/10/2013" .Please correct me where I am doing the mistake.
In Database table values is in this formate : 2013-06-30 and im retrieving this data through bean i.e. odb.getCloseDate(). Now i need to display this date in this formate i.e 23/10/2013.
HSSFCell row1 = row.createCell((short) j);
//row1.setCellType(row1.CELL_TYPE_NUMERIC);
try
{
//row1.setCellValue( Date.parse(odb.getCloseDate()));
DateFormat formatter;
Date date;
formatter = new SimpleDateFormat("dd/MM/yyyy");
row1.setCellValue(date = formatter.parse(odb.getCloseDate()));
}
catch (Exception e)
{
row1.setCellValue(odb.getCloseDate());
}
Date and Time Patterns
mm indicates Minutes, MM indicates Month
formatter = new SimpleDateFormat("dd/MM/yyyy");
instead of
formatter = new SimpleDateFormat("dd/mm/yyyy");
Try this,
String dateValue = "2013-06-30"; //consider as your date.
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");// First apply the patern to the incoming date value. Because it doesnt know the actual incming format
Date actualDate = simpleDateFormat.parse(dateValue);
simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
System.out.println(simpleDateFormat.format(actualDate));
In your date format mm should be MM
Reason
m Minute in hour
where as
M Month in year
You need Month in year not Minute in hour.
Then your for formatter becomes
formatter = new SimpleDateFormat("dd/MM/yyyy");
Have a look on different formats here.
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));
I was trying to format a string into date.
For this I have written a code:-
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
System.out.println(sdf.format( cal.getTime() ));
This is fine..
But now I want to convert a string into a date formatted like above..
For example
String dt="2010-10-22";
And the output should be like this:-
2010-10-22T00:00:00
How do I do this?
String dt = "2010-10-22";
SimpleDateFormat sdfIn = new SimpleDateFormat("yyyy-MM-dd");
ParsePosition ps = new ParsePosition(0)
Date date = sdfIn.parse(dt, pos)
SimpleDateFormat sdfOut = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
System.out.println(sdfOut.format( date ));
This should do it for you, remember to wrap it in a try-catch block just in case.
DateFormat dt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
try
{
Date today = dt.parse("2010-10-22T00:00:00");
System.out.println("Your Date = " + dt.format(today));
} catch (ParseException e)
{
//This parse operation may not be successful, in which case you should handle the ParseException that gets thrown.
//Black Magic Goes Here
}
If your input is going to be ISO, you could also look at using the Joda Time API, like so:
LocalDateTime localDateTime = new LocalDateTime("2010-10-22");
System.out.println("Formatted time: " + localDateTime.toString());
The same class you use for output formatting of dates can also be used to parse dates on input.
SimpleDateFormat reference
To use your example, to parse the sample date:
String dt = "2010-10-22";
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(dateFormatter.parse(dt));
The fields that are not specified (ie. hour, minutes, etc) will be 0. So your same code can be used to format the date on output.
Date Format Example
Containing the Conversion of String Date object from one format to another