convert String "30-MAR-07" to date object - java

SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy");
dt = formatter.parse(temp[0]);
gives me the error
java.text.ParseException: Unparseable date: "30-MAR-07"
Is there any way that I can format the given String to a Date object without having to write my own string splitting and translating-to-months-in-numerals methods? Thanks

Use correct format, as follows,
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yy");

Your SimpleDateFormat pattern is completely incorrect for "30-MAR-07". It's got the values in the wrong order, it uses the wrong separator, and the wrong month format. You want:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yy");
You may also want to specify the locale, e.g.
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yy", Locale.US);
I've checked that this works - at least on my machine :)

Try this... (new SimpleDateFormat("dd-MMM-yy")
System.out.println(new SimpleDateFormat("dd-MMM-yy").format(new Date("30-MAR-07")));

Related

how to convert date to this format 2015-02-27T15:14:13-06:00

I need to convert date to this format 2015-02-27T15:14:13-06:00
I have tried with SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX"); , but this one generating as 2015-04-09T10:39:19-04
Tks
You can either use the new Yoda time which has ISO date formatters and parsers or you can use a SimpleDateFormat of "yyyy-MM-dd'T'HH:mm:ssZ" which gives you "ccyy-mm-ddThh:mm:ss+zzzz" and insert the extra colon manually.
Something like:
new StringBuilder(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").format(date)).insert(22,':').toString();
thanks oldcurmudgeon. I needed to set timezone and add your logic to insert : at 22nd position
DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
fmt.setTimeZone( TimeZone.getTimeZone("US/Central"));
System.out.println("date to str:"+(new StringBuilder(fmt.format(new Date())).insert(22,':')));

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 SimpleDateFormat gives parse error

I am doing the following
SimpleDateFormat formatter = new SimpleDateFormat("MM-dd-yyyy");
calendar.setTime(formatter.parse("01/26/2012");
When I do
calendar.getTime()
I see
java.text.ParseException: Unparseable date: "01/26/2012"
What is am I doing wrong?
Thank you
Erm, you give the pattern
MM-dd-yyyy
to SimpleDateFormat then offer it
01/26/2012
to parse. That's not the format you specified, and it's telling you it can't parse it.
You would need:
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
You've set the format to
MM-dd-yyyy
And the you expect it to parse the string on a format: MM/dd/yyyy
Try
new SimpleDateFormat("MM/dd/yyyy");

How to convert a string to date format in java?

How to convert a String to DateFormat in java?
I am writing a application where I want to convert a string "20050105000200" to "2005-01-05 00:02:00". Is there a direct way to do it in Java? I want both input and output in String. Please let me know if there is a way to do it directly.
Can you give me some simple codes?
Thanks.
You can use SimpleDateFormat to parse the input date, and then again to format the output
SimpleDateFormat inFmt = new SimpleDateFormat("yyyyMMddHHmmss");
SimpleDateFormat outFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = inFmt.parse("20050105000200");
System.out.println(outFmt.format(d));
You should first parse it to a date like this:
http://www.exampledepot.com/egs/java.text/parsedate.html
and then format it again like this:
http://www.exampledepot.com/egs/java.text/formatdate.html
This example might help you
String str_date="11-June-07";
DateFormat formatter ;
Date date ;
formatter = new SimpleDateFormat("dd-MMM-yy");
date = (Date)formatter.parse(str_date);
Use a DateFormat to parse() this String to a Date object. Use a different DateFormat to format() the Date to the String representation you want.
See this
You can use simpledateformat class for doing that
Use SimpleDateFormat. Haven't tried on my IDE, but it goes something like this:
SimpleDateFormat fromUser = new SimpleDateFormat("yyyyMMddHHmmss");
SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String reformattedStr = myFormat.format(fromUser.parse("20050105000200"));
System.out.println(reformattedStr);

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