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

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:

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

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());

    }
 }

Edited 7 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

This article has been dead for over six months. Start a new discussion instead.