2

I want to create a Date object without a TimeZone (eg : 2007-06-21). Is this possible?

When I use the following method it prints like Thu Jun 21 00:00:00 GMT 2007

SimpleDateFormat  sdf       = new SimpleDateFormat("yyyy-MM-dd");

TimeZone          timeZone  = TimeZone.getTimeZone("GMT");
timeZone.setDefault(timeZone);
sdf.setTimeZone(timeZone);

Date              pickUpDate = sdf.parse("2007-06-21");
System.out.println(pickUpDate);
ironwood
  • 8,936
  • 15
  • 65
  • 114
  • A date does not have a format. – Sotirios Delimanolis May 23 '14 at 05:48
  • 2
    exact duplicate [java program to get the current date without timestamp](http://stackoverflow.com/questions/2806360/java-program-to-get-the-current-date-without-timestamp), additional duplicate candidates are http://stackoverflow.com/questions/5050170/java-getting-date-without-time?rq=1 and http://stackoverflow.com/questions/4772425/format-date-in-java?rq=1 – Oleg Estekhin May 23 '14 at 06:19
  • @DharshanSithamparam Please search Stack Overflow before posting. – Basil Bourque Apr 30 '16 at 00:05

7 Answers7

5

If you want to format a date, you need to use DateFormat or something similar. A Date is just an instant in time - the number of milliseconds since the Unix epoch. It doesn't have any idea of time zone, calendar system or format. The toString() method always uses the system local time zone, and always formats it in a default way. From the documentation:

Converts this Date object to a String of the form:

dow mon dd hh:mm:ss zzz yyyy

So it's behaving exactly as documented.

You've already got a DateFormat with the right format, so you just need to call format on it:

System.out.println("pickUpDate" + sdf.format(pickUpDate));

Of course it doesn't make much sense in your sample, given that you've only just parsed it - but presumably you'd normally be passing the date around first.

Note that if this is for interaction with a database, it would be better not to pass it as a string at all. Keep the value in a "native" representation for as much of the time as possible, and use something like PreparedStatement.setDate to pass it to the database.

As an aside, if you can possibly change to use Joda Time or the new date/time API in Java 8 (java.time.*) you'll have a much smoother time of it with anything date/time-related. The Date/Calendar API is truly dreadful.

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

This is the toString() of the java.util.Date

    public String toString() {

        // "EEE MMM dd HH:mm:ss zzz yyyy";
        BaseCalendar.Date date = normalize();

        StringBuilder sb = new StringBuilder(28);
        int index = date.getDayOfWeek();
        if (index == gcal.SUNDAY) {
            index = 8;
        }

        convertToAbbr(sb, wtb[index]).append(' ');            // EEE
        convertToAbbr(sb, wtb[date.getMonth() - 1 + 2 + 7]).append(' ');  // MMM
        CalendarUtils.sprintf0d(sb, date.getDayOfMonth(), 2).append(' '); // dd

        CalendarUtils.sprintf0d(sb, date.getHours(), 2).append(':');   // HH
        CalendarUtils.sprintf0d(sb, date.getMinutes(), 2).append(':'); // mm
        CalendarUtils.sprintf0d(sb, date.getSeconds(), 2).append(' '); // ss

        TimeZone zi = date.getZone();
        if (zi != null) {
            sb.append(zi.getDisplayName(date.isDaylightTime(), zi.SHORT, Locale.US)); // zzz
        } else {
            sb.append("GMT");
        }

        sb.append(' ').append(date.getYear());  // yyyy

        return sb.toString();
    }

So, if you will pass a Date and try to print it this will be printed out all the time.

ironwood
  • 8,936
  • 15
  • 65
  • 114
1

Code:

Date date = new Date();     
System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(date));

Date : Fri Apr 29 04:53:16 GMT 2016

Sample Output : 2016-04-29

Imports required :

import java.util.Date; //for new Date()
import java.text.SimpleDateFormat; // for the format change
Ani Menon
  • 27,209
  • 16
  • 105
  • 126
0
System.out.println("pickUpDate " + sdf.format(pickUpDate));

You can use the above code to get formatted Date as String

aksh1t
  • 5,410
  • 1
  • 37
  • 55
mahesh
  • 1,311
  • 1
  • 13
  • 26
0

Date isn't a date. It's a timestamp. That's some impressive API design, isn't it?

The type you need is now java.time.LocalDate, added in Java 8.

If you can't use Java 8, you can use ThreeTen, a backport for Java 7.

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
0

Use this Code:

SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date pickUpDate =sdf.parse("2007-06-21");
System.out.println("pickUpDate "+sdf.format(pickUpDate));

Hope it'll help you.

0
 String your_format_date=sdf.format(pickUpDate);
 System.out.println("pick Up Date " + your_format_date);
hari
  • 1,874
  • 1
  • 16
  • 10