简体   繁体   中英

How to convert a string which is received in unix format 1432191600 to only day?

I am getting this json data in the form of a date in unix timestamp , and i want to show it as only day names in listview , is it possible to achieve it ?

list: [
        {
         dt: 1432191600,
         temp: {
         day: 33.53,
         min: 25.92,
         }
        }
     ]

and i have converted it to like 21-05-2015 , but i want to just show the day names for the whole week.

String date = c.getString(TAG_DATE);
long dt = Long.parseLong(date);
Date date = new Date(dt * 1000L);
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); 
sdf.setTimeZone(TimeZone.getTimeZone("GMT-4"));
String formattedDate = sdf.format(date);

Can anybody give any suggestion ? Thank You

Try to use the Date(long miliseconds) constructor overload to create a date instance.

Initializes this Date instance using the specified millisecond value. The value is the number of milliseconds since Jan. 1, 1970 GMT.

source: Date class reference

then pass it to a calendar constructor and use the DAY_OF_WEEK property to retreive the day as integer.

Date myDate = new Date(yourLinuxTimestamp);
Calendar c = Calendar.getInstance();
c.setTime(yourDate);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);

i'm sure there is a better solution without haveing to create a Date instance .. just check the Calendar Class reference

Thanks guys for replying but i got what i wanted

Instead of:

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");

I changed it to:

SimpleDateFormat sdf = new SimpleDateFormat("EEEE");

which returns Thursday , Friday , Saturday , Monday , Tuesday

@Maroun Maroun SimpleDateFormat sdf = new SimpleDateFormat("E"); is also right which returns Thu , Fri , Sat , Mon , Tue

Try out below code:

   String m_date="1432191600";
   SimpleDateFormat myDate = new SimpleDateFormat("dd-MM-yyyy");
    String m_parsed = myDate.format(new Date(Long.parseLong(m_date) * 1000L));
    System.out.println("Date:====== " + m_parsed); 
    try {
        Date m_day = myDate.parse(m_parsed);

        CharSequence m_daysGet = DateFormat.format("EEEE", m_day.getTime());
        System.err.println("Days are=============" + m_daysGet);
} catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
String dt = <epoch_time>;
Date date = new Date(Long.parseLong(dt) * 1000);
System.out.println(date);

It returns:

Thu May 21 10:00:00 IDT 2015

For your input.

If you want to only display the day, you can:

SimpleDateFormat simpleDateformat = new SimpleDateFormat("E"); 
System.out.println(simpleDateformat.format(date));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM