Reputation: 14179
I have a date, and I'm trying to print out correctly, but without any success.
Here is the code I use
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
System.out.println(sdf.format(my_date));
where my_date
is a Date
object. The problem is that I always receive a date like this
Sat Jun 06 07:00:00 CEST 2015
Even if I have requested a date like this
dd/MM/yyyy HH:mm
Where is the mistake?
EDIT:
I'm working on Java EE 7 environment with Glassfish installed. I have in my MySQL database a TIMESTAMP field like this 2014-06-18 07:00:00
. I get the value and print out through EL expression in a JSP page.
This is the entity where time fields are
public class Route implements Serializable
{
...
@Column(name = "DEPARTURE_DATE", nullable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date departure_date;
@Column(name = "ARRIVAL_DATE", nullable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date arrival_date;
...
I do a query, and get the result list of routes, and through EL expression I print all the field
<c:forEach items="${routes_list}" var="route">
<tr>
<td>${route.airlane}</td>
<td>${route.aircraft_id}</td>
<td>${route.airport_city_source.city}</td>
<td>${route.airport_city_dest.city}</td>
<td>${route.departure_date}</td>
<td>${route.arrival_date}</td>
<td>${route.travel_class}</td>
</tr>
</c:forEach>
Upvotes: 2
Views: 439
Reputation: 338181
TIMESTAMP
in MySQL has an offset of zeroI have in my MySQL database a TIMESTAMP field like this 2014-06-18 07:00:00.
No, you don’t.
The TIMESTAMP
type in MySQL records a moment as seen with an offset from UTC of zero hours-minutes-seconds.
You neglected to mention the context of an offset. A string representing a TIMESTAMP
value would be described as 2014-06-18 07:00:00+00:00 or 2014-06-18 07:00:00Z where the Z
is an abbreviation for an offset of zero, +00:00
.
As an update to this old Question…
You are using terribly-flawed date-time classes that are now legacy, supplanted by the modern java.time classes defined in JSR 310.
Retrieve your TIMESTAMP
value from MySQL using the OffsetDateTime
class, as mapped in JDBC 4.2 and later.
OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class );
Note that Jakarta Persistence (formerly JPA) has been updated to support java.time classes.
Upvotes: 0
Reputation: 1
Simple Date format is locale aware, and is able to format and parse dates according to format and locale. To avoid common errors when date string is formatted using different locale and different timezone you should specify these values before you format or parse the date string.
Locale locale = new Locale("en_US");
TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm", locale);
sdf.setTimeZone(tz);
Now you can format
and parse
dates regardless of server default locale and timezone settings. You can adjust it to your locale and timezone used in your place.
Upvotes: 2
Reputation: 46841
You are taking it in wrong way. Whenever you format a date then it doesn't save the format any where in date object itself.
If you simply print the date object again then it uses default toString()
implementation of date object that's what you are getting in output.
You have to format it again whenever needed not just once.
Please have a look at my another posts here and here that might help you.
Upvotes: 0
Reputation: 534
I think this would do what you need.
Date now = new Date();
System.out.println(DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(now));
Here is a link to more references.
http://javatechniques.com/blog/dateformat-and-simpledateformat-examples/
Upvotes: 0
Reputation: 199205
You're probably printing some other date.
I tried your code here:
$ cat Hello.java
import java.text.*;
import java.util.*;
public class Hello {
public static void main( String ... args ) {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
System.out.println(sdf.format(new Date()));
}
}
And got:
$ java Hello
15/06/2014 14:10
Upvotes: 1