print the time,date and time zone in java program and change the time zone

Lets see how java uses the Timezone set on JVM. Lets print the current time and date of the existing time zone.
And then change the time zone to different time zone and observe the changes.
The below code will do the above purpose.
    public void testMain(Object[] args)
        {
            Date dt = new Date();
            System.out.println("Todays Date and time:"+dt);
            Calendar c = Calendar.getInstance();
            System.out.println("Current System timeZone : "+c.getTimeZone());
            System.out.println("current time : "+c.getTime());
            System.out.println("Current time in millis : "+c.getTimeInMillis());
            //Lets change the JVM time zone and see
            TimeZone cst = TimeZone.getTimeZone("CST");
            cst.setDefault(cst);
            System.out.println("new time zone: "+cst);
            Calendar curCal=Calendar.getInstance(cst);
            //System.out.println("New time Zone : "+curCal.getTimeZone());
            System.out.println("New time with : "+curCal.getTime());
            System.out.println("New Time in millis: " +curCal.getTimeInMillis());
            //Some other time operations      
            Timestamp timeNow = new Timestamp(curCal.getTimeInMillis());
            System.out.println("Time stamp type of timeNow= " + timeNow);
            sleep(10);
            //After some time
            c = Calendar.getInstance();
            System.out.println("Current timeZone : "+c.getTimeZone());
            System.out.println("current time : "+c.getTime());
            System.out.println("current time in millis : "+c.getTimeInMillis());
            //To pass the current local time format and display like that
            SimpleDateFormat DF = new SimpleDateFormat();
            System.out.println("Current sys time format : "+ DF.format(c.getTime()));
            SimpleDateFormat DF1 = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss");
            System.out.println("Change the Current sys time format : "+ DF1.format(c.getTime()));
        }
Corresponding output is as follows :
    Todays Date and time:Thu Mar 11 12:21:59 IST 2010
    Current System timeZone : sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null]
    current time : Wed Mar 03 15:47:38 IST 2010
    Current time in millis : 1267611458390
    new time zone: sun.util.calendar.ZoneInfo[id="CST",offset=-21600000,dstSavings=3600000,useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=CST,offset=-21600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]]
    New time with : Wed Mar 03 04:17:38 CST 2010
    New Time in millis: 1267611458406
    Time stamp type of timeNow= {SysUpTime = 146714 days 6:56:24} {Timestamp = Wed Mar 03 04:17:38 CST 2010}
    Current timeZone : sun.util.calendar.ZoneInfo[id="CST",offset=-21600000,dstSavings=3600000,useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=CST,offset=-21600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]]
    current time : Wed Mar 03 04:17:48 CST 2010
    current time in millis : 1267611468406
    Current sys time format : 3/3/10 4:17 AM
    Change the Current sys time format : 2010-03-03 04.17.48
Explanation:
So by observing the output and code lines we can understand that the time changes according to Timezone used.


0 comments to "print the time,date and time zone in java program and change the time zone"

Post a Comment

Whoever writes Inappropriate/Vulgar comments to context, generally want to be anonymous …So I hope U r not the one like that?
For lazy logs, u can at least use Name/URL option which doesn’t even require any sign-in, The good thing is that it can accept your lovely nick name also and the URL is not mandatory too.
Thanks for your patience
~Krishna(I love "Transparency")

Popular Posts

Enter your email address:

Buffs ...

Tags


Powered by WidgetsForFree

Archives