-1

I'm trying to get it to be 12 hours ahead of what it is, it doesn't work but anything lower works, unsure on how to fix it.

package New;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class DateManipulation {

public static void main(String[] args) {
    Date date = new Date();
    String test = new SimpleDateFormat("MM-dd-yy").format(date);
    System.out.println(test);
    DateFormat sdf = new SimpleDateFormat("hh:mm:ss");
    try {
        date.setTime(date.getTime()+43200000l);
        String time = sdf.format(date);
        System.out.println(time);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

HMU With any solutions

INeedHelp
  • 13
  • 4
  • Btw I've tried making a long variable with the value of 43200000l then tried adding but still didn't working, been stuck on this for 30 minutes and haven't found anything. – INeedHelp Feb 22 '16 at 23:24
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/9/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes built into Java 8 & Java 9. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Mar 25 '18 at 18:52

2 Answers2

4

The first thing to note is h in SimpleDateFormat represents the "Hour in am/pm (1-12)"

So, 10am + 12 is 10pm, but since you've not supplied a formatter for the am/pm marker, it's basically just 10...

Now days, the preferred method would be to use Java 8's Time API or Joda-Time, for example...

LocalDateTime dateTime = LocalDateTime.now();
System.out.println(DateTimeFormatter.ofPattern("MM-dd-yy HH:mm:ss").format(dateTime));
LocalDateTime later = dateTime.plusHours(12);
System.out.println(DateTimeFormatter.ofPattern("MM-dd-yy HH:mm:ss").format(later));

Which outputs...

02-23-16 10:27:27
02-23-16 22:27:27

I've deliberately used HH to show that it works

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

Try the new Date&Time API (java.time) of Java 8 and later. It is much more simple. See Tutorial.

LocalTime time = LocalTime.now(); 
LocalTime newTime = time.plusHours(12);
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
E_d
  • 132
  • 2
  • 9
  • 1
    But it they use a format of `hh:mm:ss` in `DateTimeFormatter`, they'll still have the same problem – MadProgrammer Feb 22 '16 at 23:28
  • As you said "_since you've not supplied a formatter for the am/pm marker, it's basically just 10_" – E_d Feb 22 '16 at 23:36
  • 1
    So, the point been, the answer isn't "use Java 8's Time API" (although highly recommended), the answer is, use a better format ;) – MadProgrammer Feb 22 '16 at 23:59
  • 1
    Also, I suggest always specifying the desired/expected time zone when calling `.now` rather than implicitly relying on the JVM’s current default time zone. `LocalTime.now( ZoneId.of( "America/Montreal" ) )` – Basil Bourque Feb 23 '16 at 00:35