GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing codescracker.com — Java Program to Print Date and Time
Java Program to Print Date and TimeThis article covers multiple programs in Java that prints the current date and time. Here are the list of programs covered in this article:
Get and Print Current Date in JavaThe question is, write a Java program to print the current date. The program given below is its answer. This program prints the complete current date in default format. import java.util.Date; public class CodesCracker { public static void main(String[] args) { Date d = new Date(); System.out.println("Date: " +d); } } The snapshot given below shows the sample output produced by above program, on printing the current date: Here is another way to create a program in Java that does the same job as of previous program using now(). import java.time.LocalDateTime; public class CodesCracker { public static void main(String[] args) { System.out.println(LocalDateTime.now()); } } Sample Output of Previous ProgramPrint Only Date in Specified Format in JavaThis program only prints the current date, in of course, a specified or given format. I've used the format / to separate day, month, and year. You can use any other character to format the date such as dash (-), comma (,) etc. import java.util.*; import java.text.*; public class CodesCracker { public static void main(String[] args) { Date d = new Date(); SimpleDateFormat a = new SimpleDateFormat("dd/MM/yyyy"); System.out.println("Date: " +a.format(d)); } } Sample Output of Previous ProgramPrint Only Time in Specified Format in JavaThis program prints the current time only, in again specified format. import java.util.*; import java.text.*; public class CodesCracker { public static void main(String[] args) { Date d = new Date(); SimpleDateFormat a = new SimpleDateFormat("hh:mm:ss a"); System.out.println("The Current Time is " +a.format(d)); } } Sample Output of Previous ProgramPrint Today's Day in JavaThis program prints today's day in Java. import java.util.*; import java.text.*; public class CodesCracker { public static void main(String[] args) { Date d = new Date(); SimpleDateFormat a = new SimpleDateFormat("E"); System.out.println("Today is " +a.format(d)); } } Sample Output of Previous ProgramPrint Current Time, Day, and Date in JavaThis is the last program of this article, created after combining the previous three programs, to find and print the current time, day, and date. import java.util.*; import java.text.*; public class CodesCracker { public static void main(String[] args) { Date d = new Date(); SimpleDateFormat a = new SimpleDateFormat("E"); SimpleDateFormat b = new SimpleDateFormat("hh:mm a"); SimpleDateFormat c = new SimpleDateFormat("dd-MM-yyyy"); System.out.println("Today is \"" +a.format(d)+ "\""); System.out.println("The Current Time is \"" +b.format(d)+ "\""); System.out.println("The Current Date is \"" +c.format(d)+ "\""); } } Sample Output of Previous ProgramSame Program in Other Languages« Previous Program Next Program » Practice Quizzes
Test your programming and computer science knowledge with interactive MCQs and topic-based quizzes. Popular Topics
Explore the most popular programming and computer science tutorials on CodesCracker. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |