-3

i want to change datetime type for example dd.MM.yyyy i parsed json and i want to change datetime format. I wrote some code and able to change datetime format but time is always same,

for example 2.3.2014 this is a my code

Calendar cal = new GregorianCalendar();
    DateFormat df = new SimpleDateFormat("dd.MM.yyyy");
    String date = df.format(cal.getTime());

    String DateTimeTxt = itemList.get(MainActivity.KEY_pubDate);
    DateTimeTxt = date;

    DateTime.setText(DateTimeTxt);

what is a problem ?

Sanket Shah
  • 4,352
  • 3
  • 21
  • 41

4 Answers4

2

I tried with your code. It is ok for date format convertion, I think.

Calendar cal = new GregorianCalendar();
    SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy");
    String date = df.format(cal.getTime());

    String DateTimeTxt = "2.3.2014";
    DateTimeTxt = date;

    ((TextView) findViewById(R.id.tv_date)).setText(DateTimeTxt);

The output is : 03.02.2014

So I think your problem is to get actual data on below line of your code :

String DateTimeTxt = itemList.get(MainActivity.KEY_pubDate);

Print this string and identify what you want and what you get.

Shahinoor Shahin
  • 685
  • 3
  • 14
  • thank you but i loged this String and also itemList.get(MainActivity.KEY_pubDate).please look at picture : (http://postimg.org/image/ygjgi6ym1/) Format Date Time always 03.02.2014 – user3233500 Feb 03 '14 at 10:16
1

Try like this.

   Calendar cal = new GregorianCalendar();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date = df.parse(itemList.get(MainActivity.KEY_pubDate));

        DateFormat df1 = new SimpleDateFormat("dd.MM.yyyy hh:mm:ss");
        DateTime.setText(df1.format(date));

You are formatting the current date but not the date you received from JSON.

abbas.aniefa
  • 2,855
  • 21
  • 30
0
Calendar ctaq = Calendar.getInstance();
        ctaq.setTimeInMillis(ctaq.getTimeInMillis());
        SimpleDateFormat dfaq = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
        String datetimestr = dfaq.format(ctaq.getTime());

Use this for change the format.Its working for me

Deepak Jangir
  • 580
  • 5
  • 13
  • i changed code but i can format datetime but all datetime is same .i run project and all items' datetime is similar 2.3.2014 – user3233500 Feb 03 '14 at 09:58
0

Just like time you can change you date in different format See here.

Community
  • 1
  • 1
Sagar Shah
  • 4,272
  • 2
  • 25
  • 36