开发者

Current date should add 12 hours more in android

In my application i want to add 12 hours with the current date and time,then i have to show开发者_运维百科 it in this format "yyyy-MM-dd HH:mm:ss". I wrote the code but unable to add 12 hours. How can i do? please help.

My code is :

Calendar cal = Calendar.getInstance();
            Date date = cal.getTime();
            String date1 = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(date);
            m_tvTrackEnd.setText(date1);


The Calendar class has the add method which you can use to add certain units.

Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, 12);
Date date = cal.getTime();
String date1 = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(date);
m_tvTrackEnd.setText(date1);


How about

        Calendar cal = Calendar.getInstance();    
        Date date = cal.getTime()+12*60*60*1000;
        String date1 = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(date);
        m_tvTrackEnd.setText(date1);


You should add the 12 hours to the Calendar object like this:

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.HOUR, 12)
        Date date = cal.getTime();
        String date1 = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(date);
        m_tvTrackEnd.setText(date1);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜