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()));
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));
public class DefaultDateFormatPattern {
public static void main(String[] args) {
System.out.println(new Date());
}
}
The output is: Thu Jan 08 10:52:56 IST 2015
Is there any method in Java to get the pattern of the date it is showing ?
According to Java 8 toString() method of Date class the format is: EEE MMM dd HH:mm:ss zzz yyyy.
You can try SimpleDateFormat.toLocalizedPattern() or SimpleDateFormat.toPattern() to get the pattern.
SimpleDateFormat format=new SimpleDateFormat();
System.out.println(format.toLocalizedPattern());
System.out.println(format.toPattern());
Pass locale to get the locale specific pattern.
Read similar post
Calendar rightNow = Calendar.getInstance();
DateFormat d = new SimpleDateFormat("DDD", Locale.getDefault());
String day = d.format(rightNow.getTime());
You can use the overloaded constructor of SimpleDateFormat to get the current default use Locale.getDefault() in the 2nd argument..
Check the SimpleDateFormat Api Docs
You can use SimpleDateFormat to get whatever the date format you want.
The default format of SimpleDateFormat is
d/M/yy h:mm a
you can set the way you want.
Eg.
SimpleDateFormat format=new SimpleDateFormat("dd/MM/yyyy");
System.out.println(format.format(new Date()));
I only have a date string, and I want to see the time in other TimeZone by it. So I did it like that:
String dateStr = "2014-05-15 16:14:58 PM";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");
sdf.setTimeZone(TimeZone.getTimeZone("America/Denver"));
Date date = sdf.parse(dateStr);
System.out.println(date);
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");
System.out.println(sdf1.format(date));
This is the current TimeZone in my computer:
The result that the code ran was that:
Fri May 16 06:14:58 CST 2014
2014-05-16 06:14:58 AM
The result is wrong, I had the right result by changing the TimeZone to "America/Denver" in my computer, and I saw that:
America/Denver —— 2014-05-15 02:14:58 AM
I don't know why it likes that?
But if I had a Date not a date String, I do that :
public static String getFormatedDateString(String _timeZone) {
TimeZone timeZone = null;
if (StringUtils.isEmpty(_timeZone)) {
timeZone = TimeZone.getDefault();
} else {
timeZone = TimeZone.getTimeZone(_timeZone);
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");
sdf.setTimeZone(timeZone);
// TimeZone.setDefault(timeZone);
return sdf.format(new Date());
}
System.out.println("America/Denver —— " + getFormatedDateString("America/Denver"));
The result likes that:
------Asia/Shanghai------
2014-05-15 16:32:04 PM (current date)
America/Denver —— 2014-05-15 02:32:04 AM
This result is right.
So I was confused, I could't find the problem when I just have a date string and I want to know the time of other TimeZone. Could any body help me?
Date object in Java is independent of the concept of timezone.
What you want to do get the equivalent time in another timezone of a date string which is 'supposed' to be in your own timezone.
However, 2nd point appears backwards in your code:
String dateStr = "2014-05-15 16:14:58 PM";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");
sdf.setTimeZone(TimeZone.getTimeZone("America/Denver"));
Date date = sdf.parse(dateStr);
What these 4 lines do is consider the date string as a point in time in "America/Denver" timezone.
When you parse it to the date object, it would give you the equivalent time in your own timezone.
You want it the other way round:
Hence staying close to your code (you can just use a single SimpleDateFormat instance effectively, which you can figure out later),
Drop the setTimezone on the first sdf:
String dateStr = "2014-05-15 16:14:58 PM";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");
//sdf.setTimeZone(TimeZone.getTimeZone("America/Denver"));
Date date = sdf.parse(dateStr);
System.out.println(date);
Add the same setTimezone to the other sdf:
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");
sdf1.setTimeZone(TimeZone.getTimeZone("America/Denver"));
System.out.println(sdf1.format(date));
Now, you are parsing your date String to a date in your current (JVM's) timezone. Then format the same date to a different timezone's String.
Output I get with the changed code (my JVM's timezone being IST):
Thu May 15 16:14:58 IST 2014 // Parsed the date string in IST
2014-05-15 04:44:58 AM // Equivalent time in Denver
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.
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.