Showing posts with label time. Show all posts
Showing posts with label time. Show all posts

Write a java program to return a time in hh:mm:ss format?

Question:
How to write a java program to return a time in hh:mm:ss format?

Answer:
Format formatter = new SimpleDateFormat( "hh:mm:ss" );

Code:
import java.text.Format ;
import java.text.SimpleDateFormat ;
import java.util.Date ;
 
public class PrintTime {
  public static void main( String[] args ) {
    Format formatter = new SimpleDateFormat( "hh:mm:ss" );
    System.out.println( formatter.format( new Date() ) );
    }
}

Output:
$ java PrintTime
09:10:12

How to print out the current Date and Time?

Question:
How can I print out the current date and time?

Answer:
By using the SimpleDateFormat class.

Code:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
 
 
public class CurrentDate {
 
   public static void main(String[] args) {
 
      Date date = new Date();
      DateFormat df = new SimpleDateFormat("E MM-dd-yyyy hh:mm:ss aa");
 
      System.out.println(df.format(date));
 
   }
}

Output:
$ java CurrentDate 
Sun 02-16-2014 03:29:40 PM