how to convert string into time format and add two hours

后端 未结 9 1016
轻奢々
轻奢々 2020-12-13 05:04

I have the following requirement in the project.

I have a input field by name startDate and user enters in the format YYYY-MM-DD HH:MM:SS.

相关标签:
9条回答
  • 2020-12-13 05:34

    Try this one, I test it, working fine

    Date date = null;
    String str = "2012/07/25 12:00:00";
    DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    date = formatter.parse(str);
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(Calendar.HOUR, 2);
    System.out.println(calendar.getTime());  // Output : Wed Jul 25 14:00:00 IST 2012
    

    If you want to convert in your input type than add this code also

    formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    str=formatter.format(calendar.getTime());
    System.out.println(str);  // Output : 2012-07-25 14:00:00
    
    0 讨论(0)
  • 2020-12-13 05:34

    Basic program of adding two times:
    You can modify hour:min:sec as per your need using if else.
    This program shows you how you can add values from two objects and return in another object.

    class demo
    {private int hour,min,sec;
        void input(int hour,int min,int sec)
        {this.hour=hour;
        this.min=min;
        this.sec=sec;
    
        }
        demo add(demo d2)//demo  because we are returning object
        {   demo obj=new demo();
        obj.hour=hour+d2.hour;
        obj.min=min+d2.min;
        obj.sec=sec+d2.sec;
        return obj;//Returning object and later on it gets allocated to demo d3                    
        }
        void display()
        {
            System.out.println(hour+":"+min+":"+sec);
        }
        public static  void main(String args[])
        {
            demo d1=new demo();
            demo d2=new demo();
            d1.input(2, 5, 10);
            d2.input(3, 3, 3);
            demo d3=d1.add(d2);//Note another object is created
            d3.display();
    
    
        }
    
    }
    

    Modified Time Addition Program

    class demo {private int hour,min,sec; void input(int hour,int min,int sec) {this.hour=(hour>12&&hour<24)?(hour-12):hour; this.min=(min>60)?0:min; this.sec=(sec>60)?0:sec; } demo add(demo d2) { demo obj=new demo(); obj.hour=hour+d2.hour; obj.min=min+d2.min; obj.sec=sec+d2.sec; if(obj.sec>60) {obj.sec-=60; obj.min++; } if(obj.min>60) { obj.min-=60; obj.hour++; } return obj; } void display() { System.out.println(hour+":"+min+":"+sec); } public static void main(String args[]) { demo d1=new demo(); demo d2=new demo(); d1.input(12, 55, 55); d2.input(12, 7, 6); demo d3=d1.add(d2); d3.display(); } }
    0 讨论(0)
  • 2020-12-13 05:35

    Use the SimpleDateFormat class parse() method. This method will return a Date object. You can then create a Calendar object for this Date and add 2 hours to it.

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = formatter.parse(theDateToParse);
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.add(Calendar.HOUR_OF_DAY, 2);
    cal.getTime(); // This will give you the time you want.
    
    0 讨论(0)
提交回复
热议问题