0

My server response "Nov 27, 2011 07:00am". Then I put this variable.

String fecha="Nov 27, 2011 07:00am"

but I need in this format:

String fecha="2014-11-27 07:00am";

Then I want to save this string in SQLite as Date.

How can I resolve this?

Alex K
  • 8,269
  • 9
  • 39
  • 57
Gian123
  • 1
  • 3
  • Please search StackOverflow before posting. Your questions have been handled on hundreds of other Questions and Answers. – Basil Bourque Jan 12 '15 at 05:29

2 Answers2

0

In sqllite you cannot store date and time directly, so you are left with 2 options
1) Convert it into milliseconds
2)Save it as a string.

I personally prefer the first one as it it more elite, here's the code to do the above .

    public static long convertDateToMilliSeconds(String inputDate){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
            Date date = null;
            try {
                date = simpleDateFormat.parse(inputDate);
            } catch (ParseException e) {
                e.printStackTrace();
                //throw new IllegalAccessException("Error in parsing date");
            }
            return date.getTime();
        }
     public static Date convertMilliSecondToDate(long milliSeconds){
       Date date= new Date(milliSeconds); 
       return date;
    }
Aniruddha K.M
  • 7,361
  • 3
  • 43
  • 52
0

Read this doc about SimpleDateFormat and parsing Date Time. You can even use a longer route that involves splitting the string, and re-arranging it into the format that you desire.

Omatt
  • 8,564
  • 2
  • 42
  • 144