I am trying to convert a String value that is stored in a database,for example "2012-01-20", to be in the format January 20, 2012.
I have seen some examples, but they are using Date which works with SimpleDateFormat.
As an example here is one way I tried but the "try" always fails and the result is null
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
Date convertedDate=null;
try {
convertedDate = df.parse(datePlayed);
} catch(ParseException e){
e.printStackTrace();
}
In short, you're not using the right format for parsing. You need to use two DateFormat instances; one for parsing and one for formatting.
DateFormat parser = new SimpleDateFormat("yyyy-MM-dd");
DateFormat formatter = new SimpleDateFormat("MMM dd, yyyy");
Date convertedDate = parser.parse(datePlayed);
String output = formatter.format(convertedDate);
Date da = new Date();
SimpleDateFormat ft = new SimpleDateFormat("E, dd/MM/yyyy !");
System.out.println("Update : " + ft.format(da));
You can change your date style do you want at: E, dd/MM/yyyy !
Good luck !
If the Date is read from a Database, then store it as a java.sql.Date. You can then use SimpleDateFormat on it, maybe after converting to java.util.Date. From the ResultSet object, you can extract Dates.
If what you meant is that you are given a date in text that was extracted from a DB by someone else and you are stuck with it. Try using:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
df.setLenient(true);
convertedDate = df.parse(datePlayed.trim() );
Also try displaying the text you are parsing before you parse to make sure the datePlayed value is what you expect.
With parseInt, an extra space before or after the data will cause an error, so calling trim() removes extra spaces.
Related
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();
}
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 am trying to format dates entered by my application user using SimpleDateFormat but I always get an error:
01/28/2014java.text.ParseException: Unparseable date: "01/28/2014"
The code I am using to format the date is as follows:
Date rDate, dDate;
String Date1 = request.getParameter("Date1");
String Date2 = request.getParameter("Date2");
//Here the date get display for example as 01/29/2014 (i.e. MM/DD/YYYY)
System.out.println("Date1:: "+ Date1);
System.out.println("Date2:: "+ Date2);
SimpleDateFormat parseRDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat parseDDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
//#########Crashes in the next two lines#########...
rDate = (Date)parseRDate.parse(Date1);
dDate = (Date)parseDDate.parse(Date2);
} catch (ParseException e) {
e.printStackTrace();
}
Can someone please help me by telling me what I am doing wrong here?
Thanks for your time
You need to match the DateFormat pattern to your input String
SimpleDateFormat parseRDate = new SimpleDateFormat("MM/dd/yyyy");
Any idea how I can convert the format from MM/dd/YYYY to yyyy-MM-dd HH:mm:ss?
All you need to do is use a separate SimpleDateFormat instance for formatting
SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(output.format(date1));
As you say, Date1 is of the form MM/dd/yyyy... but you're trying to parse it with a format of yyyy-MM-dd HH:mm:ss.
The pattern you parse to the SimpleDateFormat constructor has to match the format of the data itself. (What did you think you were specifying in the constructor?)
Note that the code you've provided isn't doing and formatting at all - just parsing.
You should also work out which time zone you're interested in, and which Locale. Personally I think it's clearer to specify both of those explicitly - even if you want the system default ones.
(If you're doing any significant amount of date/time work, you should also consider using Joda Time, which is a much more pleasant date/time API. I'd also consider more useful exception handling, and following Java naming conventions...)
You specify the format yyyy-MM-dd HH:mm:ss in the constructor and then accept a completely different format (MM/dd/yyyy) as input. You need to make the actual format match the expected format.
Examine the following (for comparison):
Date rDate, dDate;
SimpleDateFormat parseRDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat parseDDate = new SimpleDateFormat("MM/dd/yyyy");
try {
rDate = (Date)parseRDate.parse("2014-01-28 12:22:22");
dDate = (Date)parseDDate.parse("01/28/2014");
} catch (ParseException e) {
e.printStackTrace();
}
The string passed to the constructor is what tells SimpleDateFormat how to read the input you give it later.
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