Date format in jsp - java

I need to convert the date from request parameter to string in dateformat 'yyyy-MM-dd'.
I have tried the following
String MyDate = request.getParameter("DayCal");
formatdate = new java.text.SimpleDateFormat("yyyy/MM/dd");
Date date = (Date) formatdate.parse(MyDate);
String DisplayDate= formatdate.format(date);
But i am getting incorrect results
if the request.getParameter("DayCal") = 01/01/2010; the DisplayDate = 0006/07/03
Please help...
Thanks in advance

You will need two SimpleDateFormats - one for parsing and one for formatting:
String MyDate = request.getParameter("DayCal");
SimpleDataFormat parseDate = new java.text.SimpleDateFormat("dd/MM/yyyy");
SimpleDataFormat formatDdate = new java.text.SimpleDateFormat("yyyy-MM-dd");
Date date = (Date) parseDate.parse(MyDate);
String DisplayDate= formatDate.format(date);
(I added dashes instead of slashes, because that was your initial requirement).

From your code, request.getParameter() returns date as dd/MM/YYYY or mm/dd/yyyy, but your SimpleDateFormat() is given yyyy/MM/dd parameters which doesnot match MyDate.
Instead SimpleDateFormat() should be given dd/mm/yyyy or mm/dd/yyyy.

Related

How to converted timestamp string to a date in java

I have a string "1427241600000" and I want it converted to "yyyy-MM-dd" format.
I have tried, but I am not able to parse it, please review the below code
try {
String str = "1427241600000";
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
Date date =sf.parse(str);
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}
I would like to know where I went wrong.
You should try it the other way around. First get the Date out of the milliTime and then format it.
String str = "1427241600000";
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date(Long.parseLong(str));
System.out.println(sf.format(date));
the conversion is highly dependent on what format the timestamp is in. But i assume the whole thing should actually be a long and is simply the systemtime from when the timestamp was created. So this should work:
String str = ...;
Date date = new Date(Long.parseLong(str));
Use Date date =new Date(Long.parseLong(str)); to convert your String to Date object.
if you are using SimpleDateFormat() the format specified as a parameter to this function should match the format of the date in the String (str in your case). In your case yyyy-MM-dd does not match the format of the time stamp (1427241600000).
You can do it like this:
use a SimpleDateFormat with an appropriate format string (be careful to use the correct format letters, uppercase and lowercase have different meanings!).
DateFormat format = new SimpleDateFormat("MMddyyHHmmss");
Date date = format.parse("022310141505");

Why does my date parsing return a weird date?

Here is the code I am using:
String string = "08/07/2013".replace('/', '-');
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(string);
Why does date return: "Wen Jan 3 00:00:00 EST 14"? It is not at all the date format I told it to use.
Edit: I need this format because a database I am using requires this format.
The format you use to parse the date string, does not match it. You are using yyyy for 08.
Use the following format:
new SimpleDateFormat("dd-MM-yyyy")
and why at all are you replacing the / with -? You can build the pattern for your original string only:
String string = "08/07/2013"
Date date = new SimpleDateFormat("dd/MM/yyyy").parse(string);
and if you want your date string in yyyy-MM-dd format, then you can format the date using DateFormat#format(Date) method:
String formattedDate = new SimpleDateFormat("yyyy-MM-dd").format(date);
See also:
Change date format in a Java string
SimpleDateFormat Java Doc
When you are specifying some Simple Date format using string e.g. "yyyy-MM-dd" , you have to provide your date in same format to get a date object eg. "1991-07-24" .
String mydate = "1991/07/24";
Date formattedDate = new SimpleDateFormat("yyyy/MM/dd").parse(mydate);
now if you want to convert it in any other format, you can do that by FORMATTING this date object into that perticular format..
String dateInOtherFormat = new SimpleDateFormat("dd-MMM-yyyy").format(formatteddate);
and the output of dateInOtherFormat will be ... 24-JUL-1991 .
Ok my sugestion is stupid but if you need this format try this
String[] arr = "08/07/2013".split("/");
String newString = arr[2]+"-"+arr[1]+"-"arr[0];
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(newString);
NOTE If your original strin is in format "MM/dd/YYYY use this:
String newString = arr[2]+"-"+arr[0]+"-"arr[1];

How to convert a date from a Datepicker to Mysql DATETIME format using java?

I have a date (05/15/2013) which is from a HTML Datepicker. I want to save this value in a mySQL column, which is the type of DATETIME. Format should be yyyy-MM-dd.
you can use this
STR_TO_DATE
STR_TO_DATE(string, '%d/%m/%Y')
You can specify the format as per your requirement
You could use joda time with date formatters like this:
DateTime dateTime = DateTime.parse("05/15/2013", DateTimeFormat.forPattern("mm/dd/yyyy"));
String result = dateTime.toString(DateTimeFormat.forPattern("yyyy-mm-dd"));
You can use java.text.SimpleDateFormat class, e.g.
String pattern = "dd/MM/yyyy";
SimpleDateFormat formatter = new SimpleDateFormat(pattern);
Date today = new Date();
String output = formatter.format(today);
You can find more on official Oracle tutorial page.
You can try this
String new_date_string = your_date_string.replace("/", "-");
System.out.println(str);
or you can use regex
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(new_date_string);

convert string into date format in java

I want to convert this string to the following date format.
String s = "2-26-2013";
Date date = new SimpleDateFormat("EEEE, MMMM/dd/yyyy").parse(s);
System.out.println(date);
I'm getting this error:
Exception in thread "main" java.text.ParseException: Unparseable date: "2-26-2013"
at java.text.DateFormat.parse(DateFormat.java:357)
Well yes. The argument you pass into the constructor of SimpleDateFormat says the format you expect the date to be in.
"EEEE, MMMM/dd/yyyy" would be valid for input like "Tuesday, February/26/2013". It's not even slightly valid for "2-26-2013". You do understand that you're parsing the text at the moment, not formatting it?
It looks like you want a format string of "M-dd-yyyy" or possibly "M-d-yyyy".
If you're trying to convert from one format to another, you need to first specify the format to parse, and then specify the format to format with:
SimpleDateFormat parser = new SimpleDateFormat("M-dd-yyyy");
SimpleDateFormat formatter = new SimpleDateFormat("EEEE, MMMM/dd/yyyy");
Date date = parser.parse(input);
String output = formatter.format(date);
Date date = new SimpleDateFormat("MM-dd-yyyy").parse(s);
The argument to SimpleDateFormat defines the format your date it in. The above line matches your format, and works. Your example does not match.
Instead of using MMMM/dd/yyyy you need to used MM-dd-yyyy. SimpleDateFormat expects the pattern to match what its trying to parse.

Date conversion in Java

How can I take a string in a format such as: 2008-06-02 00:00:00.0 and convert it to: 02-Jun-2008?
Can I somehow take the original string, convert it to a Date object, then use a formatter to get the final output (rather than parsing the string myself)? Thanks!
You can use SimpleDateFormat to convert between a String and a Date object and vice versa based on a pattern. Click the API link, you'll see patterns being explained in detail. A 4-digit year can be represented with yyyy, a 3-character month abbreviation can be represented with MMM and so on.
First you need to parse the String of the first format into a Date object:
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date date = sdf1.parse(inputString);
Then you need to format the Date into a String of the second format:
SimpleDateFormat sdf2 = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
String outputString = sdf2.format(date);
Note that you need to take the Locale into account as well to get the month to be printed in English, else it will use the platform's default locale to translate the month.
Use 2 instances of SimpleDateFormat class. One for converting your input string to date and second to convert date back to string but in another format.
Here is an example of using SimpleDateFormat.
DateFormat startFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
DateFormat endFormat = new SimpleDateFormat("dd-MM-yyyy");
String outputString = null;
try {
Date date = startFormat.parse(inputString);
outputString = endFormat.format(date);
} catch(ParseException pe) {
throw new IllegalArgumentException(inputString + " is not properly formated.", pe);
}
You can definitely use SimpleDateFormat class like others have recommended.
Another suggestion if it applies in your case. If you are getting this data from a sql query you can also use to_char() method to format it in the query itself. For example: to_char(column_name,'DD-MON-YYYY')

Categories