Java.util.date time is different from system time

My system time is 12:23, but if I try to display the time using regular Java code,

Date dt = new Date(); System.out.println("date is "+dt); 

I get different times: date is Mon Sep 05 01:23:18 BDST 2011 Help me.

+4
source share
4 answers

Jdk 6 has a date issue. When I fixed the problem with jdk 7, it is solved.

+1
source

Is this right now?

 Date dt = new Date(); System.out.println(DateFormat.getDateTimeInstance().format(dt)); 

Is BDST (British Double Daylight Saving Time) your time zone (GMT + 01: 00)?

+1
source

This is a bit confusing, but you can do the following (obviously, replace your time zone):

 Calendar cal = Calendar.getInstance( TimeZone.getTimeZone("America/Los_Angeles") ); Date dt = cal.getTime(); System.out.println( dt); 

I have not tried it, but I have a feeling that it might work! Date will always be initialized in UTC.

+1
source

This can help...

 import java.util.Calendar; import java.text.SimpleDateFormat; public class DateUtils { public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; public static String now() { Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW); return sdf.format(cal.getTime()); } public static void main(String arg[]) { System.out.println("Now : " + DateUtils.now()); } } 
-1
source

All Articles