通过获取用户输入在日历中添加时间

时间:2014-10-16 06:34:38

标签: android calendar datetime-format

您可以使用日历方法帮助增加时间吗?在我的布局中,我有一个时钟和数字sorrounds每个数字是setOnTouchListener。我想要做的是,如果用户点击每个数字,它将增加10个小时。例如,当用户触摸数字1时,时间将变为11。

如果有其他方式,请向我展示如何。谢谢

这是我的活动代码

String timeOut = "2:00";//set the time
int time1 = Integer.parseInt(timeOut); //converting the string into integer 

Calendar cld = cld.getInstance();
cld.add(Calendar.HOUR, 10); //in this line here I want to add the variable time1

tetxtView.setText("You added time is equal to " + cld.getTime());

2 个答案:

答案 0 :(得分:0)

根据您的说法,我了解用户点击2所以,您需要显示12。

所以只需添加10 + time1 = 10 + 2 = 12。

String timeOut = "2:00";//set the time
int time1 = Integer.parseInt(timeOut);

Calendar cld = cld.getInstance();
cld.add(Calendar.HOUR, 10 + time1); //in this line here I want to add the variable time1

现在你会看到,12是你的时间。

让我知道这是否解决了你的问题..

答案 1 :(得分:0)

试试这个:

SimpleDateFormat sdf = new SimpleDateFormat(“HH:mm”);

    Date date = null;
    try {
        date = sdf.parse("12:22");
        System.out.println("Date "+date);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(Calendar.HOUR, 10);
    System.out.println("Calendar hour "+ sdf.format(calendar.getTime())); //Just format it as i have added this line