DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   JSP (http://www.daniweb.com/forums/forum24.html)
-   -   How to get the current date in JAVA (http://www.daniweb.com/forums/thread51208.html)

KilluaX Jul 28th, 2006 12:41 pm
How to get the current date in JAVA
 
Hi, all

Can anybody tell me how to get the current date in a java program?

And, I want to insert the date into a database with a date/time format, is there any difference from insert a string?


thanks for reply

peter_budo Jul 29th, 2006 2:45 pm
Re: How to get the current date in JAVA
 
to get date and time check this tutorial

and here you can find all you need about manipulating date/time in MySQL
date/time type

sorry couldn't help you more with previous problem been away, but looks like you sorted out :cheesy:

KilluaX Jul 30th, 2006 6:59 am
Re: How to get the current date in JAVA
 
thanks, guy you are really kind

peter_budo Jul 30th, 2006 8:07 am
Re: How to get the current date in JAVA
 
lol not kind, i'm just giving you tips where I go when I run out of ideas/get desperate etc. If you know what you are looking for it is not so bad to locate it on internet

good luck with your project

chinnuk Nov 30th, 2009 2:34 pm
import java.util.Calendar;
import java.text.SimpleDateFormat;

public class Example
{
  public static final String DATE_FORMAT_NOW = "dd-MM-yyyy HH:mm:ss";
                                                //or yyyy-MM-dd
       
        // 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[] args)
    {

 
    //Step1:Getting Time Zone "country"

    Calendar cal = Calendar.getInstance();     
    String timeZone =  cal.getTimeZone().getDisplayName();
   
        System.out.println("timeZone is : " + timeZone );
   
        //Step2:Getting Time in Milli Seconds

        System.out.println("Current Time in MiliSeconds  : " + cal.getTimeInMillis() );
 
   
        //Step3:Getting current time in date format

      //Calendar cal = Calendar.getInstance();
      SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);

    String time=sdf.format(cal.getTime());


    System.out.println(time);
    // System.out.println("Now : " + Example.now());

    }
 }


All times are GMT -4. The time now is 4:17 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC