Date Format - Java - java

I want to accept a Date format according to an ISO (can't remember which one)...
2009-09-17T13:03:00
How do I do this? I'm currently using a SimpleDateFormat but when I run my unit test against it, it fails.
DateFormat df = SimpleDateFormat("yyyy-MM-ddTHH:mm:ss");
Unit Test is passing it this string:
String test1 = "2009-09-17T13:07:01";

The SimpleDateFormat parameter should be "yyyy-MM-dd'T'HH:mm:ss".
Regards.

You need to enclose the T in single quotes

Your format is wrong. It should be something like this,
SimpleDateFormat isoFormat =
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");

An easy Solution is to remove the "T" ;-)
...
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
Date date = df.parse("2009-09-17T13:07:01".replace("T",""))
Regards, Jan

Related

How to parse a Java Date to customized string format like "17^th of Jan 2014" ('th' is a superscript)

I want to parse a java Date object to the format like below
I tied the below codes
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
DateFormat dfz = new SimpleDateFormat("dd MMM yyyy");
Date date = new Date();
String d = dfz.format(date);
System.out.println(d);
then the out put will be like
I checked this SO question and using that I could get the below format
Is there any built-in functionality in java to parse a date object to the first where the th is a superscript format or do I need to do it manually ? And how to do it manually ?
What is the easiest way to do this ?
Based on the UI you are using you need to handle super script separately.
for HTML simply use tag 17th (17<sup>th</sup>)

java.text.ParseException: Unparseable date: "130625143100"

this is my code:
SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
Date date = sdf.parse("130625143100");
i have a long list of date strings, all in the same format as above one,
wierd thing is some success, some fail.
can any one help out with WHY this happens.
thanks a lot.
You said, some string parses well while some string fails. Put your code in trycatch block. Log the string for which it is failing. The string may not be as per the format you specified in SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
Date date = sdf.parse("130625143100");
mistake in doblue quote
You may need to validate your strings, for example if the value of month is greater than 12 then it will fail or for similar reasons.

Format time with French style java

Is there a way to format the time to look like this:
10h23
I used the SimpleDateFormat and saw all the patterns available, but I want to know if there's a way to have the time like that or if I will have to build it myself.
I also tried
DateFormat tf = DateFormat.getTimeInstance(DateFormat.FULL, Locale.FRANCE);
but it prints like 11 h 01 CST
Thanks in advance.
You can use SimpleDateFormat:
DateFormat format = new SimpleDateFormat("hh'h'mm");
System.out.println(format.format(new Date()));
Printed:
12h11

Date(dateFormat.parse("string date").getTime()) Error

i have a string date 2020-01-14 00:00:00.0
when i convert this string date using
new java.sql.Date(dateFormat.parse("2020-01-14 00:00:00.0").getTime())
this will result the date as 0019-07-13
i am not getting how it is converting the string ...
pls help me
thanks
DateFormat.parse() is abstract, and SimpleDateFormat.parse() is +- 120 lines long. Presumably you're using SimpleDateFormat to do your parsing. SimpleDateFormat is initialised using the format specified in your current Locale by default if you do not pass a specific format string to your call to SimpleDateFormat's constructor.
ensure that you initialise dateFormat as follows
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SS");
and your issues should go away.

String to Date in a PreparedStatement

I am trying to use setDate() in a PreparedStatement, however the date that I have is in the format of 2008-07-31. The code is:
pstmt.setDate(f++, (Date) DateFormat.getDateInstance(DateFormat.DEFAULT).parse(value.substring(0, 10)));
However, it gives me the error:
Exception in thread "main" java.text.ParseException: Unparseable date: "2008-07-31"
Why is this?
If you have a very specific date, don't ask Java to use a default date format - set it yourself.
For example:
DateFormat parser = new SimpleDateFormat("yyyy-MM-dd");
Date date = parser.parse(value.substring(0, 10));
You should also potentially set the time zone of the parser... my guess is that UTC is the most appropriate time zone here.
Note that this has nothing to do with prepared statements as such - it's just date parsing.
(As an alternative to using DateFormat and SimpleDateFormat, you could use Joda Time which has a nicer API and thread-safe formatters/parsers. You can ask Joda Time to convert from its own types to Date values. Possibly overkill if you only need it for parsing here, but if you're doing anything else with dates, it's well worth looking into.)
You need make sure the default DateFormat is in yyyy-MM-dd format (usually it's a config in OS), or you can use SimpleDateFormat or java.sql.Date to parse date string.
java.util.Date d;
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
d = sdf.parse ("2008-07-31");
// or
d = java.sql.Date.valueOf ("2008-07-31");
or, you could just set parameter as String, if the underlying database driver support the VARCHAR/CHAR to DATE conversion.
DateFormat.DEFAULT points to MEDIUM format and MEDIUM format looks like Jan 12, 1952. So, you may have create a SimpleDateFormat object with the format you are using.
I think there is mismatch in the format of the date that you are providing as input and the format in which you have specified while formatting which is default in your case.
SimpleDateFormat parser = new SimpleDateFormay("yyyy-MM-dd");
Try using the same format for both the dates.
First convert String to Date and then set that to PreparedStatement. Check with below code.
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
Date convertedDate = dateFormat.parse(dateString);
I'd use
pstmt.setDate(f++,
new java.sql.Date(
new SimpleDateFormat("yyyy-MM-dd")
.parse(value.substring(0, 10))
.getTime()
)
);

Categories