Converting String to Date formate in xls using java - java

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.

Related

Incorrect UNIX timestamp from String Date

I am trying to convert a date in String format into UNIX timestamp, I am able to convert it but when I check the timestamp it displays incorrect date.
I am using the following code to convert a Date in String to Unix timestamp:
String selected_date = "16/11/2015 1:34 am";
datetime.setText(selected_date);
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yy hh:mm a");
Date date = null;
try {
date = dateFormat.parse(selected_date);
} catch (ParseException e) {
e.printStackTrace();
}
long unixTime = (long)date.getTime()/1000;
The output UNIX timestamp is: 1460309640
But when I convert that timestamp using a web tool it returns: 4/11/2016, 1:34:00 AM
The format
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yy hh:mm a");
is not compatible with the string
String selected_date = "16/11/2015 1:34 am";
16 can't be a month!
2015 is not a year in two digits format
The right format seems to be (not sure if kk or KK depending on hours if 0 based or not)
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy kk:mm a");

How to format a jQuery date using SimpleDateFormat

I am using jQuery Datepicker that is giving the date like 07/05/2015 this format.I am using simpledateformat to format this date.But always the SDF is converting it to the todays date.How to solve this ??
System.out.println("Activity IS : IS With Date");
SimpleDateFormat sdfOverTimeWithDate = new SimpleDateFormat("yyyy-MM-dd ");
Date startDate = ParamUtil.getDate(resourceRequest, "startDate", sdfOverTimeWithDate);
Date endDate = ParamUtil.getDate(resourceRequest, "endDate", sdfOverTimeWithDate);
int jobId= ParamUtil.getInteger(resourceRequest, "jobId");
System.out.println("jobId :"+jobId);
System.out.println("startDate :"+startDate);
System.out.println("endDate :"+endDate);
This statDate and endDate is giving todays's date only ,while the date i am pssing is in the format 07/05/2015.How to solve this ??somebody plaese help
You are passing wrong date format to SimpleDateFormat constructor. Try "dd/MM/yyyy" instead of "yyyy-MM-dd "
Is "07/05/2015" in July (US-Format) or May? Please clarify. If it is US-format (date expression starting with month number) then your solution is to use the pattern "MM/dd/yyyy" otherwise you should use the pattern "dd/MM/yyyy".
The pattern "yyyy-MM-dd " cannot be right due to two reasons:
a) It starts with a four-digit-year but your input begins with a two-digit-number.
b) It contains a trailing space.
Try to do it.
SimpleDateFormat sdfOverTimeWithDate = new SimpleDateFormat("yyyy-MM-dd");
try {
Date d = sdfOverTimeWithDate.parse("07/05/2015");
} catch (ParseException e) {
e.printStackTrace();
}

How to get Formated date?

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.

Get time from entire date

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));

How to format a date String into desirable Date format

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

Categories

Resources