How to get the current date time in Java

Source: Internet
Author: User
Tags date now


Calendar cal = Calendar.getinstance ();
Java.text.SimpleDateFormat SDF = new SimpleDateFormat ("yyyy");
String CDate = Sdf.format (Cal.gettime ());


1. With Java.util.Calendar
Calendar CA = Calendar.getinstance ();
Date now = Ca.gettime ();
Ca.add (Calendar.day_of_month, 7);
Date fu = ca.gettime ();
Fu is a date 7 days from now.
2. In addition, the current time can be obtained with the new date (), but it does not seem to advocate the Date type of the new Java.util.Date ().


A small program that displays the system date:

Import java.awt.*;
Import java. applet.*;
Import java.util.*;

public class Showtimeapplet extends applet{
Calendar calcurrent=calendar.getinstance ();
TextArea txadate=new TextArea (5,20);
int Intday=calcurrent.get (calendar.date);
int Intmonth=calcurrent.get (calendar.month) +1;
int Intyear=calcurrent.get (calendar.year);

public void init () {
Add (txadate);
Txadate.append (intyear+ "-" +intmonth+ "-" +intday);

}

Java Display current date time

Import java.util.*;
public class Calendar
{
public static void Main (string[] args)
{
GregorianCalendar calendar=new GregorianCalendar ();
Take out the current year, month, day
int Year=calendar.get (Calendar. year);

The value of the month plus 1 makes it the customary month size (January-December)
int Month=calendar.get (Calendar. MONTH) +1;
int Today=calendar.get (Calendar. Day_of_month);

Output format sample for the current year, month, Day and week
System.out.println ("+year+"/"+month+"/"+today+");
System.out.println ();
System.out.println ("Sun Mon Tue Wed Thu Sat"); Two spaces in the middle

Set the calendar to display from the 1th current month and view the day of the one week
Calendar.set (Calendar. day_of_month,1);
int Weekday=calendar.get (Calendar. Day_of_week);

Align number 1th to the corresponding week
for (int I=calendar. Sunday;i System.out.print (""); 5 spaces in the middle
int day,month;
Using the Do-while loop to implement the output of the calendar
do{
Day=calendar.get (Calendar. Day_of_month);
System.out.print (day);

/* Date is today more output a "*" for identification, the date is not today to determine whether less than 10,
Output a certain space. This is primarily to align the formatting. */
if (day==today)
{
System.out.print ("*");
if (day<10) System.out.print ("");
}
else if (day<10) System.out.print ("");
else System.out.print ("");
Weekday=calendar.get (Calendar. Day_of_week);

According to the format requirements, Saturday to change lines. If you do not wrap, the output of a certain space
if (Weekday==calendar. SATURDAY) System.out.println ();
else System.out.print ("");

Calendar.add (Calendar. day_of_week,1);
Month=calendar.get (Calendar. MONTH) +1;
}
while (month==month);//If Month exceeds the current month, exit the loop,
System.out.println ();
System.out.println ();
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.