2

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>
Roman C
  • 49,761
  • 33
  • 66
  • 176
Mazzy
  • 13,354
  • 43
  • 126
  • 207
  • work fine for me ... can you share how you create `my_date`. here is how I create it `Date my_date = new Date(2014,1,31);` – Mzf Jun 15 '14 at 19:09
  • I have copied the code and it works. I get the right format. Are these lines in your code behind another or is it possible that sdf is changed between the to lines? – Jens Jun 15 '14 at 19:10
  • [Works for me](https://ideone.com/UktBMg). – Sotirios Delimanolis Jun 15 '14 at 19:10
  • Sorry I made a mistake. The situation is more complicated it could appear. I get the right string format but when I try to print out through EL expression I got all that date... – Mazzy Jun 15 '14 at 19:10
  • Ok @SotiriosDelimanolis I'm going to post to explain better the situation – Mazzy Jun 15 '14 at 19:36
  • @Mazzy This question is convoluted. Hone it to a *specific* issue with a specific chunk of code. Also, search StackOverflow as hundreds of similar questions have been answered. – Basil Bourque Jun 15 '14 at 20:08
  • Ok I'm posting the code – Mazzy Jun 15 '14 at 20:10
  • ok problem solved thanks to this post http://stackoverflow.com/questions/281380/format-date-with-fmtformatdate-jsp – Mazzy Jun 15 '14 at 20:15

4 Answers4

2

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.

Roman C
  • 49,761
  • 33
  • 66
  • 176
1

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
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
0

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.

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
0

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/

Will_Panda
  • 534
  • 10
  • 26