-1

I am parsing the date but not getting the proper result.

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
startDate = dateFormat.parse("2013-05-18");

getting output "Sat May 18 00:00:00 IST 2013" but i want to simple 2013-05-18 format in date format not string.

Thanks in advance...!!!

John
  • 51
  • 1
  • 3
  • 13

3 Answers3

5

If you want the string "2013-05-18" from the string "2013-05-18", you don't need any parsing at all. To transform a date object into a string with a given format, use SimpleDateFormat.format():

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
startDate = dateFormat.parse("2013-05-18");
System.out.println(dateFormat.format(startDate));

A Date object is just a wrapper around a long value holding a number of milliseconds. Its toString() method doesn't use the pattern you used to parse the string and create the Date object.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • But this will return the string and I want in date object. – John May 18 '13 at 10:07
  • 2
    A Date doesn't have any format. It's just a number of milliseconds. To represent it as a string in a given format, see my answer. Your code uses `date.toString()`, so you are also printing a string. If you expect date.toString() to return the date in your preferred format, that will never happen, because you must use a DateFormat to do that. – JB Nizet May 18 '13 at 10:09
  • I want this is in date format because i have to compare the date range if(currDate.before(startDate) || currDate.after(endDate)){} – John May 18 '13 at 10:11
  • 3
    before() and after() don't compare dates by transforming them to strings and comparing the strings. They just compare the number of milliseconds contained in the Date objects. They work fine, don't worry. If you compare the speed of two cars, you don't care whether they represent the speed using a spin in a round panel or a digital display, do you? You just care about the km/h the counters indicate. That's the same with Date object. They hold a number of ms., and you don't care about the way this number is represented. You just compare the numbers. That's what after() and before() do. – JB Nizet May 18 '13 at 10:13
2

I agree with @JB Nizet

   Date now = new Date();
   SimpleDateFormat originalFormat = new SimpleDateFormat("yyyy-MM-dd");
   System.out.println("Format :   " + originalFormat.format(now));

Output

    Format :   2013-05-18

http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date -> text), parsing (text -> date), and normalization.

For DateComparision

 public class DateComparision
 {
 public static void main( String[] args ) 
 {
    try{

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = sdf.parse("2013-4-10");
        Date date2 = sdf.parse("2013-4-30");

        System.out.println(sdf.format(date1));
        System.out.println(sdf.format(date2));

        if(date1.compareTo(date2)>0){
            System.out.println("Date1 is after Date2");
        }else if(date1.compareTo(date2)<0){
            System.out.println("Date1 is before Date2");
        }else if(date1.compareTo(date2)==0){
            System.out.println("Date1 is equal to Date2");
        }else{
            System.out.println("How to get here?");
        }

    }catch(ParseException ex){
        ex.printStackTrace();
    }
}
}

Edit:

        public static void main( String[] args ) 
{
    try{

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = sdf.parse("2013-8-16");
        Date date2 = sdf.parse("2013-8-15");

        System.out.println(sdf.format(date1));
        System.out.println(sdf.format(date2));

        if(date1.after(date2)){
            System.out.println("Date1 is after Date2");
        }

        if(date1.before(date2)){
            System.out.println("Date1 is before Date2");
        }

        if(date1.equals(date2)){
            System.out.println("Date1 is equal Date2");
        }

    }catch(ParseException ex){
        ex.printStackTrace();
    }
}

Output:

      2013-08-16
      2013-08-15
      Date1 is after Date2

Edit 2:

      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      Date date1 = sdf.parse("2013-4-10");
      Date date2 = sdf.parse("2013-5-30");
      Date currentdate = sdf.parse("2013-5-15");

      System.out.println(sdf.format(date1));
      System.out.println(sdf.format(date2));

      if(date1.compareTo(currentdate) * currentdate.compareTo(date2) > 0)
       {
                System.out.println("Current Date is between the two dates");
       }  

Output

      2013-04-10
      2013-05-30
      Current Date is between the two dates
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

Here are some useful example of SimpleDateFormat:

public class JavaSimpleDateFormatTest
{
public static void main(String[] args)
{
// (1) get today's date
Date today = Calendar.getInstance().getTime();

// (2) create our date "formatter" (the date format we want)
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-hh.mm.ss");

// (3) create a new String using the date format we want
String folderName = formatter.format(today);

// (4) this prints "Folder Name = 2009-09-06-08.23.23"
System.out.println("Folder Name = " + folderName);
}
}

More information you can find out here

Udi Oshi
  • 6,787
  • 7
  • 47
  • 65