Who is your favourite actor? Let's say it's Amitabh Bachchan. No matter what role he plays, he still remains himself (and that is why directors/producers go to him with next offer ). His acting decorates his look as per the role. What directors get using his acting skill is a decorated character representing him as per the specified role. And, then you say Amitabh Bachchan as an angry young man
OR Amitabh Bachchan as a coolie
etc.)
Similarly, no matter what kind of look you ask java.util.Date
to take (using SimpleDateFormat
), it always represents the no. of milliseconds from the epoch of 1970-01-01T00:00:00Z
. It does not have any time-zone or zone-offset information. From this milliseconds, Java calculates the date and time based on the time-zone of your JVM and then combines the obtained date and time with the time-zone of your JVM and finally returns the string when its toString()
method is called. I believe you already know that when you print an object using System.out.println
, the string returned by the object's toString()
method gets printed. Check this for more details of the toString()
implementation of java.util.Date
. A SimpeDateFormat
changes the look of java.util.Date
as per the pattern. What you get using a SimpeDateFormat
is a decorated string representing the Date
object as per the specified pattern. And, then you say Date
as a string of EEE MMMM dd, yyyy HH:mm
pattern OR Date
as a string of yyyy-MM-dd HH:mm:ss
pattern etc.
The summary is: You can not change Date
object by specifying different patterns. If you print it, you will always get the string returned by its toString()
function. If you want a different string, you can do so by using SimpleDateFormat
.
A recommendation:
I recommend you switch from the outdated and error-prone java.util
date-time API and SimpleDateFormat
to the modern java.time
date-time API and the corresponding formatting API (package, java.time.format
). Learn more about the modern date-time API from Trail: Date Time.
If your Android API level is still not compliant with Java8, check How to use ThreeTenABP in Android Project and Java 8+ APIs available through desugaring.