-2

I want to take current time irrespective to the system date. I am using Glassfish server and derby in netbeans. I tried the code below for getting current date according to IST:

DateFormat df = new SimpleDateFormat("yyyymmdd");
    df.setTimeZone(TimeZone.getTimeZone("ist"));
    String gmtTime = df.format(new java.util.Date().getTime());

    java.util.Date parsed = null;
    try {
        parsed = (java.util.Date) df.parse(gmtTime);
    } catch (ParseException ex) {
        Logger.getLogger(EmployeePanel.class.getName()).log(Level.SEVERE, null, ex);
    }
    java.sql.Date date = new java.sql.Date(parsed.getTime());

but I am getting 2017-01-08 instead of 2017-08-08.

rghome
  • 7,961
  • 8
  • 39
  • 57
  • Apart from Jens' answer, check this question too -> [Java timezone - strange behavior with IST?](https://stackoverflow.com/questions/11264697/java-timezone-strange-behavior-with-ist) – Am_I_Helpful Aug 08 '17 at 06:23

1 Answers1

1

m is minute not month (M). The right pattern is:

SimpleDateFormat("yyyyMMdd");

The pattern definition is case sensitive!

For more informations see the javadoc of SimpleDateFormat

Scary Wombat
  • 43,525
  • 5
  • 33
  • 63
Jens
  • 63,364
  • 15
  • 92
  • 104