开发者

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{
           开发者_StackOverflow     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);
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜