Convert String to date format with contains characters month - java

Good day,
Please correct me if my title and question is not correct.
Normally I just convert a String to a date format all in numeric, example :
mm/dd/yyyy to yyyy-mm-dd as 01/02/2000 to 2000-02-01
or any other style/pattern.
I am thinking can I convert it to a date format with contains of characters month or not, example :
mm/dd/yyyy to dd-MMM-yyyy as 10/09/2013 to 09-Oct-2013
Which is the month no longer display in numeric, but in alpha.
I am work in Java, Struts framework.
Kindly advise.

Hope this helps. You can choose multiple patterns which can be found at link below. Here is an example how to use it
Multiple patterns can be found here
final String OLD_FORMAT = "dd/MM/yyyy";
final String NEW_FORMAT = "yyyy/MMM/dd";
// August 12, 2010
String oldDateString = "12/08/2010";
String newDateString;
SimpleDateFormat sdf = new SimpleDateFormat(OLD_FORMAT);
Date d = sdf.parse(oldDateString);
sdf.applyPattern(NEW_FORMAT);
newDateString = sdf.format(d);
`

Related

SimpleDateFormat can not parse string to Date

I'm so ashamed asking this question, but I have no other choice.
I want to convert this string to date:
2015-11-25T19:36:39.571+06:00
To convert it I use SImpleDateFormat:
String str = "2015-11-25T19:36:39.571+06:00";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date date = format.parse(str);
System.out.println(date);
When I launch this code it gives to me java.text.ParseException: Unparseable date: exception.
I don't know why this is happening.
This should work (In java 7)
String str = "2015-11-25T19:36:39.571+06:00";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
Date date = format.parse(str);
System.out.println(date);
XXX is available in Java 7 as Timezone offset, see the Javadoc http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
The issue is because of wrong pattern used.Z isInstead of yyyy-MM-dd'T'HH:mm:ss.SSSZ use pattern yyyy-MM-dd'T'HH:mm:ss.SSSX
Z represents Time zone in RFC 822 time zone format i.e., like this -0800
So, your code will look like this.
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
Your pattern does not correspond entirely to the input string, you have a slight type in the time zone part of your format.
Change your format in the following way to make it work:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Or change your input string to:
String str = "2001-07-04T12:08:56.235-07:00";
Please use Below Pattern it works fine
"yyyy-MM-dd'T'HH:mm:ss.SSSXXX"
There is a problem with your input String according to the pattern you have used.
remove the last ':' from the str variable.
change:
String str = "2015-11-25T19:36:39.571+06:00";
in to:
String str = "2015-11-25T19:36:39.571+0600";
and run. It'll work.
Here is the fiddle:
working link

Convert .net date to Java date

In my code, I'm receiving a date formatted: /Date(-82135555200000-0800)/
How can I parse it in Java to become a Java date?
In the string /Date(-82135555200000-0800)/
use this line of code to remove non-number element from the String
String tempDate = data.getDate().replaceAll("\\D+", "");
what you actually be interested in should be 8213555520000 . you would need first 15 digits to get the date from it as the given time is in milliseconds, you can do whatever you want with it.
Many languages follow this convention so dealing with date becomes easy
Date date = new Date(Long.parseLong(""+tempDate));
Or as following
String dateAsText = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.format(new Date(821355552*10000L));

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

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

Date format in jsp

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.

Categories