1
 9/4/2014 3:55:10 AM   

here is my current date and time i want to add +10 hour so that i cam match with Current time of device please tell me how we can implement

public static void main(String[] args) {

        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        String currentDateandTime =" 9/4/2014 3:55:10 AM "; 
        Date date = formatter.parse(currentDateandTime);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR, 10);

        System.out.println("Time here "+calendar.getTime());    



    }

This Code am trying but i am not able to Impalement please help me where am doing mistake .

Edge
  • 925
  • 5
  • 15
  • 31

4 Answers4

2

Your code is almost working, but you have made a typo. You try to invoke parse() on formatter which has not been declared. Instead you have to call parse() on sdf:

public static void main(final String[] args) throws Exception {
    final SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
    final String currentDateandTime = " 9/4/2014 3:55:10 AM ";
    final Date date = sdf.parse(currentDateandTime);
    final Calendar calendar = Calendar.getInstance();

    calendar.setTime(date);
    calendar.add(Calendar.HOUR, 10);

    System.out.println("Time here " + calendar.getTime());
}

Since you are using 12-hour system you can modify it like this:

public static void main(final String[] args) throws Exception {
    final SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");
    final String currentDateandTime = "9/4/2014 3:55:10 AM";
    final Date date = sdf.parse(currentDateandTime);
    final Calendar calendar = Calendar.getInstance();

    calendar.setTime(date);
    calendar.add(Calendar.HOUR, 10);

    System.out.println("Time here " + sdf.format(calendar.getTime()));
}
stevecross
  • 5,588
  • 7
  • 47
  • 85
1

If you operate on dates after 1970, adding 10 hours to given date can be achieved in one line of code:

Date d1 = new Date(); // or sdf.parse()
Date d2 = new Date( d1.getTime() + 10 * 60 * 60 * 1000 );  // add 10h in millis

Output is:

Thu Sep 04 13:56:39 CEST 2014
Thu Sep 04 23:56:39 CEST 2014
przemek hertel
  • 3,924
  • 1
  • 19
  • 27
0

You didn't create a DateFormat. Try this:

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

public class Main {

    public static void main(String[] args) throws ParseException {

        String currentDateandTime = " 9/4/2014 3:55:10 AM ";
        DateFormat formatter = DateFormat.getInstance();
        Date date = formatter.parse(currentDateandTime);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR, 10);

        System.out.println("Time here " + calendar.getTime());

    }

}

enter image description here

Bruno Franco
  • 2,028
  • 11
  • 20
  • Type mismatch: cannot convert from java.util.Date to java.sql.Date Error is coming d – Edge Sep 04 '14 at 11:30
  • Look at the imports, change your imports. clean java.sql.Date and write java.util.Date – Bruno Franco Sep 04 '14 at 11:31
  • java.text.ParseException: Unparseable date: " 9/4/2014 3:55:10 AM " at java.text.DateFormat.parse(Unknown Source) at Test.main(Test.java:16) exception is coming can u please send complete code include import – Edge Sep 04 '14 at 11:37
0

First if you want to figure out the current time of your computers clock you can do the following:

long now = System.currentTimeMillis();

Which will return a long representing that timestamp. If you want it as a Date object you can do:

Date now = new Date();

As for the rest of your code the logic looks correct here is a snippet of my code that adds 10 hours to now.

Date now = new Date();
Calendar cal = new GregorianCalendar();
calendar.setTime(date);
calendar.add(Calendar.HOUR, 10);
Date plus10 = calendar.getTime();
System.out.println(plus10);
Matthias Steinbauer
  • 1,786
  • 11
  • 24
  • actually this time coming from server 9/4/2014 3:55:10 AM i want to match with Pc time if i will add 10 hour – Edge Sep 04 '14 at 11:32