This code gave me 09:40 (I am in Turkey) but my time is now 12:40
I looked through many pages for this, but I couldnt find any solution.
What should I do to fix it?
String DATE_FORMAT_NOW = "dd-MM-yyyy HH:mm:ss";
Calendar cal = Calendar.getInstance();
String timeZone = cal.getTimeZone().getDisplayName();
System.out.println("timeZone is : " + timeZone );
System.out.println("Current Time in MiliSeconds : " + cal.getTimeInMillis() );
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
String time=sdf.format(cal.getTime());
System.out.println(time);
Also why this code gave 08:27 when my time is 12:40
DateFormat formatterHourSecond = new SimpleDateFormat("HH:mm");
String saatStr = formatterHourSecond.format(tarih);
System.out.println("Saat : " + saatStr);
It looks like the TimeZone of your JVM is not correct. Try explicitly setting the
TimeZoneto Turkey in theSimpleDateFormatobject, like this: