-2

Am getting response as

 "pickTime": "Tue Oct 25 03:57 PM" 

i had coded as follows to get.

SimpleDateFormat curFormater = new SimpleDateFormat("E MMM dd hh:mm a");
                SimpleDateFormat dateFormater = new SimpleDateFormat("E MMM dd");
                SimpleDateFormat timeFormater = new SimpleDateFormat("hh:mm a");

String time = jsonObj.optString("pickTime");
Date dateObj = curFormater.parse(time); 
String newDateStr = dateFormater.format(dateObj); 
String newTimeStr = timeFormater.format(dateObj);

The problem is I am getting same value in String time but in dateobj I am getting value as

"sun oct 25 15.57 pm"

newDatestr as

"sun oct 25"

Please help me to solve this problem ,thanks in advance

IAmGroot
  • 13,760
  • 18
  • 84
  • 154
vimal raj
  • 295
  • 1
  • 13

4 Answers4

0

It is taking the October 25 of 1970 (Linux time).

You have to figure out how to add the year to the date because it is not taking the current year.

It takes the default one instead (the linux time starts on January 1 1970).

David Marciel
  • 865
  • 1
  • 12
  • 29
  • how to rectify it , year wont be come in response. then is it possible to solve my issue. if it so tell me. – vimal raj Oct 25 '16 at 11:12
  • Yes, use: "int year = Calendar.getInstance().get(Calendar.YEAR);" to get the year http://stackoverflow.com/questions/136419/get-integer-value-of-the-current-year-in-java – David Marciel Oct 25 '16 at 11:28
  • thanks for your help.I already done like this.But it wont be current year always because if we are in dec 2016,the pickTime may be on 2017(response).At that time it wont work. – vimal raj Oct 25 '16 at 11:40
  • Then you have to compare the date, if it is greater than the actual one it is a year less. if( Calendar.getInstance().before(dateCreated) ) { dateCreated.add(Calendar.MONTH, -1); } – David Marciel Oct 25 '16 at 13:37
0

Your response does not contains year. So you are getting incorrect output.

Below code is working try it out.

Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        SimpleDateFormat curFormater = new SimpleDateFormat("E MMM dd hh:mm a yyyy");
        SimpleDateFormat dateFormater = new SimpleDateFormat("E MMM dd");
        SimpleDateFormat timeFormater = new SimpleDateFormat("hh:mm a");

        String time = "Tue Oct 25 03:57 PM" + " " + calendar.get(Calendar.YEAR);
        Date dateObj = null;
        try {
            dateObj = curFormater.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Logger.logd(this, dateObj.toString());

        String newDateStr = dateFormater.format(dateObj);
        String newTimeStr = timeFormater.format(dateObj);
        Logger.logd(this, newDateStr + " " + newTimeStr);
subrahmanyam boyapati
  • 2,836
  • 1
  • 18
  • 28
0
  SimpleDateFormat formatter =new SimpleDateFormat("EEE MMM dd hh:mm a");
     Date date2 = formatter.parse("Tue Oct 25 03:57 PM");
    long time = date2.getTime();
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(time);
    cal.set(Calendar.YEAR, 2016);
    String formatted = formatter.format(cal.getTime());

    System.out.println(formatted);

@David Marciel is right. you should specify year.

Vinay
  • 89
  • 9
-1
SimpleDateFormat curFormater = new SimpleDateFormat("E MMM dd hh:mm a");

in this why you have put E.

SimpleDateFormat curFormater = new SimpleDateFormat("EEE MM dd yyyy hh:mm a");

I think this should be format.

Vinay
  • 89
  • 9