0

Is it possible to store the current date in a file but no as a string?

What I mean:

If I use :

SimpleDateFormat thedate = new SimpleDateFormat("dd/MM/yyyy"); 
Date d=new Date();
String formattedDate=thedate.format(d);
date.add(formattedDate);

It stores (with the format I want) in date list (which is a String List).

I want 'date' to be a Date List and store it to a file.

Is it possible to store it as dd/MM/yyyy ?Because like this:

SimpleDateFormat thedate = new SimpleDateFormat("dd/MM/yyyy"); 
Date d=new Date();
date.add(d);

it stores as

Thu Apr 18 17:06:14 GMT+03:00 2013

Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
George
  • 5,808
  • 15
  • 83
  • 160

1 Answers1

0

Try the above code:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
Date date = new Date();
dateFormat.format(date);

This will not save the date as string(as you desire) and will format the current date according to your need. I haven't tested it right now, but as I have used this before so I am pretty sure this works. Hope this helps. If it does not then please comment.

Update

I tried following:

    List<Date> d = new ArrayList<Date>();
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); 
    Date date = new Date();
    String s = dateFormat.format(date);
    try {
        d.add(dateFormat.parse(s));
    } catch (ParseException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    Log.d("try","NewDateString: "+ d.get(0));
    Log.d("try","NewDate: "+ d.get(0).getDate()+" "+d.get(0).getMonth()+" "+(d.get(0).getYear()+1900));

It works and following was the result I got:

    NewDateString: Thu Apr 18 00:00:00 EDT 2013
    New Date: 18 3 2013

So what you can do is, save the date as above and when you are retrieving the date from the file you may choose to get the Day/Month/Year from the date object that you saved.

Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • :Hello, it gives me "llegalArgumentException" at dateFormat.format(date);. – George Apr 18 '13 at 15:36
  • :First , thanks for helping.Let me ask you.I save the date as you said (I also put at the end "d.add(date)").When I load I do "SimpleDateFormat thedate = new SimpleDateFormat("dd/MM/yyyy"); Date date=new Date();" and then "d.add(date)"(I also tried not to do this last command).It still does the same.Am I missing sth here? – George Apr 18 '13 at 16:37
  • :I just show that in LogCat i receive the correct day as you,but in the file I have "Thu Apr 18 00:00:00 GMT+03:00 2013" (wrong time also!) – George Apr 18 '13 at 16:53
  • You have the wrong time because we are saving in the format of "dd/MM/yyyy". If you want to have the correct time use "dd/MM/yyyy HH:mm" – Shobhit Puri Apr 18 '13 at 17:10
  • :I think I didn't make my self clear.Leave alone the time.The problem is that in the file I save I receive "Thu Apr 18 00:00:00 EDT 2013".I want to receive "18/03/2013".Only in Logcat it appears 18 3 2013. – George Apr 18 '13 at 17:30
  • :Hello ,do you have aby ideas?Thanks! – George Apr 19 '13 at 08:29
  • I would be happy to look into it more but currently I am in middle of something. Why don't you save it as string format in form of 18/03/2013 and then obtain it as string from file? Then you can convert that string to date back again. – Shobhit Puri Apr 19 '13 at 08:44
  • :I tried!I tried to store as String List and then tried to copy this to a Date List but no success.I tried " SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); Date convertedDate=new Date(); try{ for (int k=0;k – George Apr 19 '13 at 08:51