-2

I want to convert the system date to yyyy-MM-dd format. There are similar questions in SO. I found that I need to parse the date in input format and then convert to the output format. But I am stuck at the first stage itself. I am not able to parse the system date as such (Sat Apr 25 14:44:15 IST 2015).

Here is my MWE:

import java.util.*;
import java.text.*;

public class Test
{
    public static void main(String[] args)
    {
        try
        {
            Date date = new Date();                          
            SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MM dd HH:mm:ss aaa YYYY");          
            date = dateFormat.parse(date.toString());
            System.out.println(date);
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
}

I get the exception as :

Unparseable date: "Sat Apr 25 14:53:33 IST 2015"

Java Enthusiast
  • 1,161
  • 2
  • 15
  • 30
  • 5
    You **already** have a Date object. It's thus completely useless to transform it to a String and parse that String to get back a Date object. All you need to display the Date object in yyyy-MM-dd format is to create a SimpleDateFormat, and format the Date object: `String formatted = new SimpleDateFormat("yyyy-MM-dd").format(new Date());` – JB Nizet Apr 25 '15 at 09:32
  • Have you consider using `E M D H:m:s z y`? (Assuming your code is just an example and you're not seriously trying to parse a `Date` object - Remember, a `Date` has no "format" of it's own, it's just a container for the number of milliseconds since the Unix Epoch, you use a `DateFormatter` when you want to display a `Date` in a given format) – MadProgrammer Apr 25 '15 at 09:35
  • @JB Nizet I know that but I want to return as date rather than string so have to approach like that – Java Enthusiast Apr 25 '15 at 09:38
  • `new Date()` returns the system date as a Date. You don't need anything more. – JB Nizet Apr 25 '15 at 09:39
  • @NB Nizet actually I am using sqlite db. The format of date is yyyy MM dd. I need to subtract that value with systems current date. But java has different format. So I need two date objects in the same format – Java Enthusiast Apr 25 '15 at 09:42
  • 4
    Dates do not have any format. They're just a number of milliseconds. That's like trying to take an Integer (25), transform it to a String, and then parsing that String to get back the Integer 25. Doesn't make any sense. To get the difference in milliseconds between a given Date and the system date, all you need is `date.getTime() - System.currentTimeMillis()` – JB Nizet Apr 25 '15 at 09:44
  • `MM` won't work if your month is abbreviation, use `MMM` instead. `aaa` you should look up what that means, because this is not for time-zones. Btw strings like `"Sat"` or `"Apr"` are bound to your locale. So set your locale where the "day name" or month can be written like this. – Tom Apr 25 '15 at 09:44
  • Formatted date will return string only...We cant change the format and get a date object back.. – kirti Apr 25 '15 at 09:48

3 Answers3

1

Date object can be converted to string of any date format. String can be converted to date but it will come only in standard date format's but cant be in the one as you want..

If you want to format system date to yyyy-MM-dd format then use:

Date date = new Date();
SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");          

String date1 = dateFormater.format(date);

As you specified in comment you want to subtract sql date with current date then just convert the sql date to normal date format.
Like this:

String date = your date;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");     
Date date1 = dateFormat.parse(date);

Date currentdate = new Date();

Then use calender objects:

Calendar calendar = Calendar.getInstance();
calendar.setTime(date1);

Calendar calendar2 = calendar.getInstance();
calendar2.setTime(currentdate);

long difference = (calendar2.getTimeInMillis() - calendar
    .getTimeInMillis()) / 60000;

This will give you the difference between two dates in minutes.

Tom
  • 16,842
  • 17
  • 45
  • 54
kirti
  • 4,499
  • 4
  • 31
  • 60
0

This will work for you

public class Test1 {
public static void main(String[] args) {
    try
    {
        Date date = new Date();                          
        System.out.println(date);
        String dateFormat = new SimpleDateFormat("EEE MM dd HH:mm:ss aaa YYYY").format(date);          
        System.out.println(dateFormat);
    }
    catch(Exception e)
    {
        System.out.println(e.getMessage());
    }
}
        }

Output

Sat Apr 25 15:10:38 IST 2015
Sat 04 25 15:10:38 PM 2015
Ankur Anand
  • 3,873
  • 2
  • 23
  • 44
  • actually I am using sqlite db. The format of date is yyyy MM dd. I need to subtract that value with systems current date. But java has different format. So I need two date objects in the same format – Java Enthusiast Apr 25 '15 at 09:45
  • have you tried overriding toString() method so that you get date in that particular format ? – Ankur Anand Apr 25 '15 at 09:59
  • you can't subtract date like that .. there is lots of tutorial online you says you how to subtract two dates – Ankur Anand Apr 25 '15 at 10:09
0

I think you should do it like that.

Date date = new Date();                                        
String formattedDate = new SimpleDateFormat("yyyy-MM-dd").format(date);                       
DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
date = format.parse(formattedDate);
System.out.println(date);

But you should understand difference between "date" and "date format".

Augustas
  • 1,167
  • 21
  • 31
  • @NB Nizet actually I am using sqlite db. The format of date is yyyy MM dd. I need to subtract that value with systems current date. But java has different format. So I need two date objects in the same format – Java Enthusiast Apr 25 '15 at 09:45
  • JB Nizet said correctly in comment there is no need to make Integer(25) string and then back to Int – Augustas Apr 25 '15 at 09:56
  • further reading. http://stackoverflow.com/questions/4216745/java-string-to-date-conversion date conversation – Augustas Apr 25 '15 at 09:56