简体   繁体   中英

Converting Timestamp value in a prticular Date Time Format

I have a problem that I have a timestamp value which is coming from JSON parsing, I am converting that timastamp in Date Time Format which is desired, all is working fine but month always return 1 in any case. means If timestamp contains aug month but it returns only 1 for any month. I don't know how to resolve this. Please suggest me the solution for the same.

Thanks in advance.

Code:

public String create_datestring(String timestring){
        try{
                final Calendar cal = Calendar.getInstance();
                cal.setTimeInMillis(Long.parseLong(timestring));
                timestring = String.valueOf(cal.get(Calendar.MONTH)+1) + "/" +String.valueOf(cal.get(Calendar.DAY_OF_MONTH)) + "/" + String.valueOf(cal.get(Calendar.YEAR));
        }catch(NumberFormatException e){
            timestring = "9/23/2011";--->timestring always returns 1 for month.
        } 
private String getDate(String timeStampStr){

    try{
        DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
        Date netDate = (new Date(Long.parseLong(timeStampStr)));
        return sdf.format(netDate);
    }
    catch(Exception ex){
        return dateInStr;
    }
}

Try this one It may gives you satisfied output.

String DATE_FORMAT="hh:mm aa dd MMMM yyyy";
    @SuppressLint("SimpleDateFormat")
        private String getDate(long time) {
            return new SimpleDateFormat(DATE_FORMAT).format(time * 1000);
        }

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