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. Use Java.util.Calendar
Calendar CA = Calendar.getinstance ();
Date now = Ca.gettime ();
Ca.add (Calendar.day_of_month, 7);
Date fu = ca.gettime ();
Fu is the date after 7 days
2. In addition, the current time is obtained with the new date (), but it does not seem to be advocated with the date type new Java.util.Date ()


A small program that displays the system date:

Due to the original procedure (reproduced in 2009-9-8) a little problem, so slightly modified (2009-12-2):

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

public class Showtimeapplet extends applet{
GregorianCalendar calendar=new GregorianCalendar ();
Take out the current year, month, day
int Year=calendar.get (Calendar. year);

Month value plus 1 to make it a custom month size (January-December)
int Month=calendar.get (Calendar. MONTH) +1;
int Today=calendar.get (Calendar. Day_of_month);

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

Set to display the calendar from the current month of 1th and see if he is 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 appropriate week
for (int I=calendar. Sunday;i < weekday;i++)
System.out.print (""); 5 spaces in the middle
int day,month;
Use the Do-while loop to achieve 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, date is not today judged whether it is less than 10, output a certain space. This is primarily to align the format.

if (day==today)
{
System.out.print ("*");
if (day<10) System.out.print (""); 2 spaces in the middle
}
else if (day<10) System.out.print (""); 3 spaces in the middle
else System.out.print (""); 2 spaces in the middle
Weekday=calendar.get (Calendar. Day_of_week);

According to the format requirements, Saturday to wrap. If you do not wrap, a certain space is output
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 ();

}

Reprinted from Http://blog.csdn.net/quxiuer/archive/2008/09/24/2972773.aspx

How to get the current date time in Java

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.