Why is my date not displaying properly? - java

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().

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.

Java Date Parsing As Per Format [duplicate]

This question already has answers here:
Calendar date to yyyy-MM-dd format in java
(11 answers)
Closed 7 years ago.
I need to parse the date as per format but it didn't work very well.
public class Main {
public static void main(String[] args) throws ParseException {
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateInString = "07/06/2013";
Date date = formatter.parse(dateInString);
System.out.println(date);
}
}
i need date object in 07/06/2013 format even if date was in any format. but parse method always return in Fri Jun 07 00:00:00 PKT 2013.
You can always have your date object in "dd/MM/yyyy" format - when you want to output it just use:
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
System.out.println(formatter.format(date));
The reason you see Fri Jun 07 00:00:00 PKT 2013 displayed is because System.out.println uses the default toString representation of the object you provided. For Date instances it gives you such information (depending on locale, afaik).
P.S. keep in mind that instances of SimpleDateFormat are not thread-safe so it is better to create new ones.

Difficulty parsing a string with SimpleDateFormat

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);
}

Printing date using dateformat is different when setting own date or when calling new Date ()

I am trying to print two dates using SimpleDateFormat but for my custom date the output looks completely different.
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date date2 =dateFormat.parse("01/01/2014 10:45:01");
System.out.println(("date2:"+date2));
Date date = new Date();
System.out.println(dateFormat.format(date)); // how it prints this is the desired outcome
OUTPUT:
date2:Wed Jan 01 10:45:01 GMT 2014
11/04/2014 10:45:50
The output is correct. You are creating date2 by parsing the date in string with the DateFormat. But when you print date2, you are not printing it with dateFormat.format() and hence the date is printed in its default format.
Try System.out.println("date2:"+dateFormat.format(date2));
format() will return you string of date in your desired format.
Date Object ---------->SDF Fomatter------>Formatted date in String
parse() accept the date in string format (your customize format) and return you Date Object
Formatted String date ------>SDF parse----->Date object
Wanna check, print it's value:
dateFormat.format(dateFormat.parse("01/01/2014 10:45:01"));
You have parsed it using dateformat, But you need to format it to get desired output.
System.out.println(("date2:"+dateFormat.format(date2)));
Did you try dateFormat.parse("dd/MM/yyyy HH:mm:ss"); ?
The line
System.out.println(("date2:"+date2));
Implicitly calls the toString() method on the date2 parameter. As Date has overridden the toString() method it inherits from Object, it is that method that dictates the format of the output. The Javadoc for Date#toString() states:
Converts this Date object to a String of the form:
dow mon dd hh:mm:ss zzz yyyy
Which matches what you're seeing in your output. In order to get the output that you were expecting, you would need to do the following instead:
System.out.println(("date2:" + dateFormat.format(date2)));
Date objects don't have a format associated with them. They're a dumb object that doesn't need to know about any display formatting details, as they aren't relevant to the date itself.

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.

Categories