-4

I have date in string object. I want to convert into Date object.

Date getDateFmString(String dateString)
{

 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 Date convertedCurrentDate = sdf.parse(dateString);
 return convertedCurrentDate ;
}

above function returning following output.

 Fri Apr 22 00:00:00 IST 2016
  • but I want output in this format '2016-03-01' only
  • function should take string only.
  • function should return Date object.
SHAMBANNA
  • 302
  • 2
  • 11
shimbu shambu
  • 127
  • 2
  • 10

6 Answers6

2

I have done lot of research over web, but I got solution from one Expert.

  Date getDateFrmString(String dDate)
  {       

    java.sql.Date dDate  = new java.sql.Date(new SimpleDateFormat("yyyy-MM-dd").parse(sDate).getTime());
    return dDate;
  }  

this is what I want.

shimbu shambu
  • 127
  • 2
  • 10
0

Change the date format from

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

to

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

Hope this works

singhakash
  • 7,613
  • 4
  • 25
  • 59
Reenu
  • 91
  • 1
  • 6
0

You are parsing with the wrong format try

String dateString="01-01-2016";
SimpleDateFormat sdfP = new SimpleDateFormat("dd-MM-yyyy");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date convertedCurrentDate = sdfP .parse(dateString);
String date=sdf.format(convertedCurrentDate );
System.out.println(date);

Output:

2016-01-01

DEMO1

And if you want the format to dd-MM-yyyy then no need to define seperate SimpleDateFormat object.

String dateString="01-01-2016";
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Date convertedCurrentDate = sdf.parse(dateString);
String date=sdf.format(convertedCurrentDate );
System.out.println(date);

OUTPUT:

01-01-2016

DEMO2

To format the string date you have to first parse the String to Date object using the same format of date which the String have then format it using the desired format as seen in the above code.

singhakash
  • 7,613
  • 4
  • 25
  • 59
0

See this example

public Class DateFormatDemo{ 

public static void main (String args[]) {

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy);
String dateInString = "01/01/2015";

try{

Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date)); 

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

This link might help you with string to date object conversions

Tony Vincent
  • 10,120
  • 5
  • 44
  • 60
0

Date objects don't have a format. Only a String does. A Date object will be output with whatever format you tell it to be format as. It all depends on what the format of the DateFormat object is when you call .format(). Calling the toString() method on a Date object uses a DateFormat of "dow mon dd hh:mm:ss zzz yyyy".

4castle
  • 28,713
  • 8
  • 60
  • 94
0

Let's do it step by step:

  1. You have a date as String in dd-MM-yyyy format.
  2. You want to convert it into date. (for this you are using SimpleDateFormat)
  3. Now you are printing the date. Question here is are you printing the converted date object or input string? If its a date object then toString method is called of date class.

As per comment on java.util.Date class it's:

dow mon dd hh:mm:ss zzz yyyy
similar to
Fri Apr 22 00:00:00 IST 2016

So that coincides with what you get in output in the second approach. But how is that code even running is strange.

String inputStr = "11-11-2012";
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date inputDate = dateFormat.parse(input);

Variable 'input' is not defined.

What are the possible solutions:

  1. While printing date, convert it back to String using SimpleDateFormat as per the requirement.

    Date d =new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String dStr = sdf.format(dateString);
    System.out.printn(dStr);
    
  2. Extending class java.util.Date and override toString, but that would be a bad idea.
Himanshu Bhardwaj
  • 3,798
  • 2
  • 14
  • 35
  • I want to write a function that accept string and returns date in dd-MM-yyyy format. – shimbu shambu Apr 22 '16 at 06:00
  • @shimbushambu your comment makes no sense. Date is a object, String is where formatting can be applied. So you will need to convert date to String in order to display it. Or else Date class will use its default which you already see. – Himanshu Bhardwaj Apr 22 '16 at 06:19