My input is String formated as the following:
3/4/2010 10:40:01 AM
3/4/2010 10:38:31 AM
My code is:
DateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy hh:mm:ss aa");
try
{
Date today = dateFormat.parse(time);
System.out.println("Date Time : " + today);
}
catch (ParseException e)
{
e.printStackTrace();
}
the output is:
Sun Jan 03 10:38:31 AST 2010
Sun Jan 03 10:40:01 AST 2010
I'm not sure from where the day (Sun) came from? or (AST)? and why the date is wrong? I just wanted to keep the same format of the original String date and make it into a Date object.
I'm using Netbeans 6.8 Mac version.
Should be MM, not mm. The lowercase mm is for minutes, not months.
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa");
MM, not mm for months. You are using mm twice - and logically, it's the same things - minutes.
If you want to print the date in the original format, use the format method:
System.out.println("Date Time : "+ dateFormat.format(today));
the "weird" format comes from Date's toString implementation, the javadoc says:
Converts this Date object to a String of the form:
dow mon dd hh:mm:ss zzz yyyy
"I just wanted to keep the same format of the original String date and make it into a Date object."
The Date Object is intended to represent a specific instant in time, you can't keep the format of the original string into it, that's why we have the DateFormat Class.
The answer is simple. You have displayed the Date.toString() value of today and not the intended dateFormat version. what you require is:
System.out.println("Date Time : " + dateFormat.format(today) );
Printing the Date out using System.out.println() results in the toString() method being called on the Date object.
The format string used in toString() is what is causing the Day of the week and the timezone to appear in the output.
This is apart from the parsing mistake pointed out by Duffy.
Related
public static void main(String[] args) throws ParseException {
SimpleDateFormat dt = new SimpleDateFormat("MM dd yy");
dt.setLenient(false);
dt.setTimeZone(TimeZone.getTimeZone("Asia/Hong_Kong"));
Date date = dt.parse("05 14 16");
System.out.println(date);
}
Output: Fri May 13 21:30:00 IST 2016
If i try to use the output it is switching to one day before instead of the correct day.
Is this expected or an issue with the API?
This is expected and there is no bug in Java.
Class Date does not contain timezone information. A java.util.Date is nothing more than wrapper for a number of milliseconds since 01-01-1970, 00:00:00 GMT. It does not remember that the string that it was parsed from contained information about a timezone.
When you display a Date, for example by (implicitly) calling toString() on it as you are doing here:
System.out.println(date);
it will be printed in the default timezone of your system, which is IST in your case.
If you want to print it in a certain timezone, then format it using a SimpleDateFormat object, setting the desired timezone on the SimpleDateFormat object. For example:
DateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss Z");
df..setTimeZone(TimeZone.getTimeZone("Asia/Hong_Kong"));
System.out.println(df.format(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 (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.
I have a code that looks like this:
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2013-06-03 00:00:00");
System.out.println(date);
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
System.out.println("Current Date Time : " + dateFormat.format(cal.getTime()));
it outputs:
Mon Jun 03 00:00:00 EDT 2013
Current Date Time : 2013-06-24 12:52:04
I want to change the first date printed in first line to look like the second date printed in second line. how can I do this? thanks in advance.
You cannot influence what the Date#toString method does (unless you are willing to subclass Date, which would not be advised). Simply put, don't rely on Date#toString—that's what SimpleDateFormat is for.
Well, you can see the toString method of the Date object! you will see that the toString outputs the Date object like the format bellow:
EEE MMM dd HH:mm:ss zzz yyyy
That's why DateFormat comes into the party to format the Date object, to serve our demand of formatting it as our wish!
I think you're confusing yourself because you're creating a date using SimpleDateFormat#parse specifying a mask and it's not being "kept" after printing it, right?
The point is: no matter how you create a Date object, it will always use it's default mask when you print it - that is something like Mon Jun 03 00:00:00 EDT 2013.
If you want to change the way it's printed, you could use a SimpleDateFormat, just as you did in your post:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = dateFormat.parse("2013-06-03 00:00:00");
dateFormat.format(date);
Just to be clear about it: SimpleDateFormat does not change the date object in any way. It's purpose is only to format and parse date objects.
Well, combine the two, no?
System.out.println(dateFormat.format(date));
Use the same DateFormat to print the first Date object as well.
System.out.println(dateFormat.format(date));
How, println() prints out the Date object depends on how Date.toString() has been implemented. You cannot change the format unless you extend and override the Date class which obviously, isn't the right approach.
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = dateFormat.parse("2013-06-03 00:00:00");
System.out.println(date); // formats as: EEE MMM dd HH:mm:ss zzz yyyy
System.out.println(dateFormat.format(date));
System.out.println("Current Date Time: " +
dateFormat.format(Calendar.getInstance().getTime()));
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