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

Recommended Answers

All 5 Replies

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:

thanks, guy you are really kind

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

    }
 }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.