Nithinlal
Nithinlal

Reputation: 5061

Adding 1 hr dynamically in android

I have a code

String date = 05/09/13 10.55 PM;
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy hh.mm a");
Date testDate = null;

testDate = sdf.parse(date);

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
String newFormat = formatter.format(testDate);
System.out.println(".....Date..." + newFormat);

And this gives me output as

 05/09/13 10:55:00 PM

What i actually need:

05/09/13 11:55:00 PM //i want to add an hour to the date I got

Upvotes: 2

Views: 102

Answers (2)

Rebelek
Rebelek

Reputation: 365

Date newDate = DateUtils.addHours(testDate, 1);

DateUtils.addHours

Edit:

Here u are:

String date = 05/09/13 10.55 PM;
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy hh.mm a");
Date testDate = sdf.parse(date);

Date newDate = DateUtils.addHours(testDate, 1);

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
String newFormat = formatter.format(newDate);
System.out.println(".....Date..." + newFormat);

Upvotes: 0

Pankaj Kumar
Pankaj Kumar

Reputation: 82938

Use below code This will add 1 hour and print required result.

String date = "05/09/13 10.55 PM";
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy hh.mm a");
        Date testDate = null;

        try {
            testDate = sdf.parse(date);
            // Add 1 hour logic
            Calendar tmpCalendar = new GregorianCalendar(); 
            tmpCalendar.setTime(testDate); 
            tmpCalendar.add(Calendar.HOUR_OF_DAY, 1);           
            testDate = tmpCalendar.getTime();           

            // Continue with your logic
            SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
            String newFormat = formatter.format(testDate);
            System.out.println(".....Date..." + newFormat);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Output

.....Date...05/09/2013 11:55:00 PM

Upvotes: 2

Related Questions