-2

Please help me to find a solution on this issue This is the date format I got form Json object

Sun Jan 08 00:00:00 GMT+05:30 2017.

I need to convert this date format to 2017-01-05.

I implemented some code like this

android.text.format.DateFormat df = new android.text.format.DateFormat();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = simpleDateFormat.parse(summary1.getDeliveryDate());
if(date1.equals(df.format("yyyy-MM-dd", new java.util.Date())))
{
       spinnerArray.add("Cancel");
} 

I am unable to enter in to loop... Please help me to find a solution

Avijit
  • 1,770
  • 5
  • 16
  • 34
NareshY
  • 1
  • 1

2 Answers2

0

Sample Code

    String inputDate = "2017-01-05 00:00:00";
    System.out.println("Input Date: "+inputDate);
    String pattern = "yyyy-MM-dd";
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
    Date formatDate = simpleDateFormat.parse(inputDate);
    String outputDate = simpleDateFormat.format(formatDate);
    System.out.println("Output Date: " +outputDate);

Output :

Input Date: 2017-01-05 00:00:00
Output Date: 2017-01-05

Please check and let me know.

Avijit
  • 1,770
  • 5
  • 16
  • 34
  • when I write like this " summary1.getDeliverydate() " I got a date format like this "2017-01-05 00:00:00.0" I want to eliminate this " 00:00:00.0 " – NareshY Jan 05 '17 at 09:53
  • Please store summary1.getDeliverydate() into a variable and check the datatype of the variable and then applythe above mentioned logic. – Avijit Jan 05 '17 at 10:02
  • summary1.getDeliverydate() it returns string data type.. I used your code String pattern = "yyyy-mm-dd"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); String formatDate = simpleDateFormat.parse(date); after this code execution i got date format like this Sun Jan 08 00:00:00 GMT+05:30 2017 but I need like this 2017-01-05 – NareshY Jan 05 '17 at 10:28
  • Check the above code... After execuring "Date formatDate = simpleDateFormat.parse(date)" code please use "String outputDate = simpleDateFormat.format(formatDate);" I have already mentioned the code in above . Please use that. You will get the desire output. I have also tested in eclipse. – Avijit Jan 05 '17 at 10:31
  • Please execute the code like this - SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); Date formatDate = simpleDateFormat.parse(inputDate); String outputDate = simpleDateFormat.format(formatDate); – Avijit Jan 05 '17 at 10:36
0

Maybe this works for you.

Date date1 = summary1.getDeliveryDate();
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
if(fmt.format(date1).equals(fmt.format(new Date())))
{
    spinnerArray.add("Cancel");
} 
Jaesga
  • 61
  • 4