Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How to Get the current date and time

  1. #1
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default How to Get the current date and time

    This code will print the current Date and Time to the console.

    import java.text.SimpleDateFormat;
    import java.util.Calendar;
     
    public class DateAndTime{
     
     public static final String DATE_FORMAT_NOW = "dd-MM-yyyy HH:mm:ss";
     public static String dt;
     
     public static String DateTime() 
     {
         Calendar cal = Calendar.getInstance();
         SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
         return sdf.format(cal.getTime());
     }
     
     public static void main(String[] args){
     
      DateAndTime dAT = new DateAndTime();
      dt = dAT.DateTime();
     
      System.out.println(dt);
     
     }
    }

    Output example:

    19-05-2008 13:35:07
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers


  2. #2
    Junior Member
    Join Date
    Jul 2008
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Get the current date and time

    Very good, but the time is a hour late,
    however most likely it is because we have day light saving (Summer in New Zealand)

    There are a lot of dreamer running the radio station, wanting a white (Snow) Christmas
    Last edited by Eric; December 3rd, 2008 at 12:02 AM.

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: How to Get the current date and time

    Hello Eric.

    Thank you very much for bringing that to my attention. +Rep points for you..
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. Program to print current directory path to the console
    By JavaPF in forum Java Programming Tutorials
    Replies: 1
    Last Post: October 9th, 2009, 12:59 PM
  2. Replies: 3
    Last Post: May 8th, 2009, 01:27 PM
  3. Changing colors of large image in real time
    By chals in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: May 7th, 2009, 05:06 AM
  4. Updation of webpage to the last modified date
    By Revathy G in forum Java Programming Tutorials
    Replies: 1
    Last Post: May 6th, 2009, 04:47 AM