Java parse date using SimpleDateFormat - java

I am trying to convert a English date from String format to Date and if possible get it as French format.
For example I get something in this format : "2014-07-21T14:31:08+0200"
I am using this line but this raise a ParseException any idea ?
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.Z");

You have a . in front of the Z inside the format.
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date date = simpleDateFormat.parse("2014-07-21T14:31:08+0200");
happily parses.

Related

convert java.util.Date directly into desired format? [duplicate]

This question already has answers here:
How can I get Date in MM/DD/YY format from Timestamp
(7 answers)
Closed 9 years ago.
How to convert java.util.Date directly into desired Date format ?
I have tried to convert the date into string first and then again into date but it still takes date format as Mar 10,2011, i want this format as 10-03-2011
Its simple :) and SimpleDateFormat can help you out.
But if you have datestring then yes you need to parse the datestring to date and re-format it again to desired format(dd-MM-yyyy).
OP failed to mention that he is displaying these in a JTable. You need these things for your date column in your JTable.
A CellRenderer : This will display the date in its string format (Use Simpledateformat)
A TableModel : This holds the internal representation
Now when you sort the JTable, the column will sort on the internal data representation.
Just pass in your date object directy to this method and you'll get the date in desired string format
new SimpleDateFormat("dd-MM-yyyy").format(date);
You can do like this
SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy");
String date = format.format(Date.parse(objectDate.toString()));
Create a SimpleDateFormat with the date format dd-MM-yyyy:
Date today = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
String date = dateFormat.format(today);
System.out.println("Today in dd-MM-yyyy format : " + date);
try this code.
String dateFormat = "dd-MM-yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, new Locale("en_US"));
String tDate = sdf.format(date);
System.out.println(tDate);
You can try this
Date date = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).parse(string);
for 10-03-2011 your format should be "dd-MM-yyyy"

How to convert into Timestamp format in Java?

I am getting the date time in "1/23/2013 6:57:17 AM" format and need to convert it into "2012-01-01T12:00:00" format. I could have solved the issue by using string functions and separating the date and time and dealing with them individually. But the problem is compunded by the fact that the date format is M/D/YYYY and even time has only h:mm:ss which means i cannot assume the number of characters before each delimiter.
I hope someone has dealt with something like this before. Thanks.
No, String functions are not the way to go.
I'd recommend using two DateFormat instances: one for the source format and another for the target format.
DateFormat source = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
source.setLenient(false);
DateFormat target = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
target.setLenient(false);
String dateAsString = "1/23/2013 12:00:00 AM";
Date d = source.parse(dateAsString);
System.out.println(target.format(d));

Java Date localization

How can I set the localization so that java.util.Date have the German format? ( ie: dd.mm.yyyy ) . I need to send to a Webservice a Date(), but it can be only on this format. Not as String but as a Date Object.
Thank you
A java.util.Date doesn't have a format. The format only comes into play when you parse a String as a Date or format a Date for display. Internally the date is just a long.
To format an instance of a Date object as a String you can use the SimpleDateFormat class in java.text
formatter = new SimpleDateFormatter("dd.mm.yyyy");
String formattedDate = formatter.format(new Date());
SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner.
Something like this would serve your case,
Date today;
String output;
SimpleDateFormat formatter;
formatter = new SimpleDateFormat("dd.MM.yyyy");
today = new Date();
output = formatter.format(today);
System.out.println(output);
See this for more help : Customizing Formats
As mentioned by Eric, java.util.Date does not have a format. If your Web Service returning a Date as a Date object, then the WS client should convert it to its desired format. In your case it is dd.mm.yyyy. Simple and best utility is SimpleDateFormat that formats a Date to the desired pattern and returns a String. You can't deny a String here because you need a formatted date object.

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

How to convert String object to Date object? [duplicate]

This question already has answers here:
How to convert ISO 8601 date (string) to Date?
(3 answers)
Closed 5 years ago.
How can I convert a String object to a Date object?
I think I need to do something like this:
Date d=(some conversion ) "String "
Any help would be greatly appreciated.
SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
Date date = dateFormat.parse("1.1.2001");
For details refer to: SimpleDateFormat documentation
Date-to-String conversion is a relatively complex parsing operation, not something you can do with a simple cast as you are trying.
You'll have to use a DateFormat. It can be as simple as:
Date d = DateFormat.getDateInstance().parse("09/10/2009");
But this changes the expected date format depending on the locale settings of the machine it's running on. If you have a specific date format, you can use SimpleDateFormat:
Date d = new SimpleDateFormat("d MMM yyyy HH:mm").parse("4 Jul 2001 12:08");
Note that the parse method will always expect one specific format, and will not try to guess what could be meant if it receives a different format.
See Sun's Java tutorial and the class SimpleDateFormat
Use a SimpleDateFormat with a format string, which matches your actual format:
SimpleDateFormat sdf =
new SimpleDateFormat("yyyy-MM-dd");
Date d = sdf.parse("2009-10-09");
java.text.SimpleDateFormat that extends java.text.DateFormat abstract class.
DateFormat MYDate = new SimpleDateFormat("dd/MM/yyyy");
Date today = MYDate.parse("09/10/2009");
you should parse the string with the SimpleDateFormat class
use
Date date = DateFormat.getInstance.parse( dateString );
You can convert String object into Date object using this method. and this Java code is tested and running component in my environment.
public static Date parseStringAsDate(String dateStr, String format) throws ParseException
{
if(null==dateStr || "".equals(dateStr))
throw new IllegalArgumentException("dateStr must not be null or empty");
DateFormat df = new SimpleDateFormat(format);
return df.parse(dateStr);
}
dateStr = "17/05/2017"
format= "dd/MM/yyyy"

Categories