Difficulty parsing a string with SimpleDateFormat - java

I have a date string called allianceStartDate which has the value of
"1/7/2010"
I am trying to convert this date string to util Date object. The code which I have tried is as follows:
Date d = new SimpleDateFormat("dd/mm/yyyy").parse(allianceStartDate);
However the result of this operation is:
Fri Jan 01 00:07:00 GMT 2010
The desired result is a Date object in the format: "01/07/2010".
Thanks for any help offered.

It should be "dd/MM/yyyy" not "dd/mm/yyyy", We use mm for minutes not for Month in Java. You should use MM for month.
Read more about Java SimleDateFormat.

After parsing your initial text to obtain the Date object you will need to format it usig also a date formater in order to display it as a formatted text. Here is an exemple:
SimpleDateFormat SIMPLE_DATE_FORMATTER = new SimpleDateFormat("dd/MM/yyyy");
public String toSimpleDateFormat(Date d) {
return SIMPLE_DATE_FORMATER.format(d);
}

Related

Unable converting String into Date in Java

I want to convert String into Date in Java.
I have written following code for that:
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss.SSSSSS");
Date date = dateformat.parse("2015-07-16 23:59:59.0");
dateformat.format(date);
After parsing I am getting following value for date : Fri Jan 16 23:59:59 IST 2015
I have tried many examples but didn't get proper solution.
Thanks in advance.
"mm" in the format string stands for minutes. You need to specify "MM" for the month part, or it defaults to January:
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
As you said you want to convert string to date then it will be received only in that format.
We cant format date in java we can format string representation of date using dateforamtter but if you are converting string to date it will be received in only that format that will be unformatted.

Date conversion issue in java datatype

I have a string type date. I want to change it firstly into date datatype. But it is not formating date properly as I want.Here is my code
String a="12/11/2010";
Date d=new Date();
SimpleDateFormat as=new SimpleDateFormat("dd/MM/yy");
d=as.parse(a);
System.out.println(a);
Output is like this :Sun Jan 01 00:00:00 IST 2012
But I want output like this 12-10-2014 in Date data type, not in String data type.
System.out.println(as.format(d));
You should use your dateformatter object to format your date d.
If you want to parse it as 12/11/2010 and output as 12-11-2010 you could have 2 different dateformatters one for parse and one for output. Ex.
String a="12/11/2010";
Date d=new Date();
SimpleDateFormat as=new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat as1=new SimpleDateFormat("dd-MM-yyyy");
d = as.parse(a);
System.out.println(as1.format(d));
A Date is an instant in time, and although it has a toString() that is not replaceable. Instead you use a DateFormat (like the one you have) to format the output
String a="12/11/2010";
SimpleDateFormat as=new SimpleDateFormat("dd/MM/yy");
Date d=as.parse(a);
System.out.println(as.format(d));

How can i get data in format "YYYY-MM-DD 00:00:00.0" using class Date?

How can i get data in format "YYYY-MM-DD 00:00:00.0" using class Date (it's extremly important to use exactly this class)?
I tried to do everything i can think of:
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
df.format(date)
and
String pattern = "yyyy-MM-dd HH:mm:ss.S";
SimpleDateFormat format = new SimpleDateFormat(pattern);
try {
Date date = format.parse("2011-01-18 00:00:00.0");
} catch (ParseException e) {
e.printStackTrace();
}
byt when i print date using logger i get this format "Tue Sep 30 00:00:00 MSK 1913".
Try this
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
Date date = format.parse("2011-01-18 00:00:00.0");
System.out.println(format.format(date));
Are you sure you want the hours, minutes, secs to be zeroes?
Or do you mean the pattern yyyy-MM-dd HH:mm:ss ?
The date class is always independent of the formatting. It only needs to be translated when you print it, like this:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String out = df.format(date)
System.out.println(out);
Or do you want to strip the time out of the date object? or something.
You are confused by Date.toString() and SimpleDateFormat.format()
An object of Date (java.util.Date) has no format information. If you call date.toString(), (which is called by your logger), you got default representation of this object, you have seen what it is.
However, SimpleDateFormat.format() will give you a string as return value, this value will format the Date object with a pattern defined by SimpleDateFormat.
In your code, you first parsed the string, with certain pattern, to get the date object. If it was successful, you got the Date object, here, for this date object, you don't have any format information, even if you have defined a pattern to parse the input string. If you want to print/output (to string again) the date object, you have to use the SimpleDateFormat.format() method.
I hope the below one will solve your problem when you need to do for dynamic dates.
Date today = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
String formattedDate = sdf.format(today);
System.out.println(formattedDate);
//Above line output (formatted String object) is 2017-12-29 00:00:00
System.out.println(format.format(formattedDate));
//Above line output(parsed Date object) is Fri Dec 29 00:00:00 IST 2017
For Date object you can't get the output as yyyy-MM-dd 00:00:00, but you will get this format in String object type.

SimpleDateFormat parse function changing the format

I have a String with several dates:
[20-Jul-2012 5:11:36,670 UTC PM, 20-Jul-2012 5:11:36,683 UTC PM]
ParsePosition parsePos = new ParsePosition(1);
SimpleDateFormat format2 = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss,SSS z a");
System.out.println(format2.parse(entry.getValue().toString(), parsePos)) ;
Output : Fri Jul 20 06:11:36 BST 2012
I need the output to be 20-Jul-2012 5:11:36,670 UTC PM.
Do I need to set a LOCALE in the SimpleDateFormat to not have a different output?
You need to set the time zone, but more importantly, you simply need to actually use the format to format the date:
Date date = format2.parse(...);
String formattedDate = format2.format(date);
System.out.println(formattedDate);
What your code does is:
Date date = format2.parse(...);
System.out.println(date.toString());
I don't really understand the point in parsing a string to a date, and then displaying the date using the exact same format, though (except to validate that the String is indeed a valid date, but then you could simply reuse the original string).
You've got two small problems:
Use hh for the hour, not HH. H is "Hour in day (0-23), and so will not work correctly with a, the AM/PM marker. Your two example date strings will parse to AM, not PM.
You're using SimpleDateFormat to parse the string, but not format it. Use format2.format(format2.parse(entry.getValue().toString()).
Here's a complete example:
SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss,SSS z a");
String input = "20-Jul-2012 5:11:36,670 UTC PM";
Date date = format.parse(input);
String output = format.format(date);
System.out.println(output);
Result:
20-Jul-2012 05:11:36,670 UTC PM

Why is my date not displaying properly?

I used SimpleDateFormat:
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
value.adStartDate = df.parse("2011/11/11 11:11:11");
I was hoping the date would come out like the string I provided, but instead I am getting this:
Fri Nov 11 2011 11:11:11 GMT-0500 (Eastern Standard Time)
This is showing on a form that I created using Javascript...
Is there a way to "force" the output on the form to be like the string?
Basically I want to pass a date with format "yyyy/MM/dd hh:mm:ss" to a form that was generated using Javascript and have the form display it in that same format.
One thing is parsing and another thing is formatting.
Check this example in order to display the formatted string.
#Test
public void testDateFormat() throws ParseException {
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
Date myDate = df.parse("2011/11/11 11:11:11");
System.out.println(df.format(myDate));
}
Output:
2011/11/11 11:11:11
What do you want to achieve? You have successfully parsed "2011/11/11 11:11:11" String into java.util.Date object. Date.toString() yields the string you see (Fri Nov 11 2011 11:11:11 GMT-0500 (Eastern Standard Time)).
If you now want to format the Date object back to String, use df.format() which does the opposite thing compared to df.parse().

Categories