Get year, month, day, hour, minute, and second in JSP

Source: Internet
Author: User
Tags date now

Import java. util .*;

Int y, M, D, H, mm;
Calendar c = calendar. getinstance ();
Y = C. Get (calendar. year); // year
M = C. Get (calendar. month) + 1; // month
D = C. Get (calendar. day_of_month); // day
H = C. Get (calendar. hour); // hour (hour -- twelve hour; hour_of_day -- twenty-four hour)
Mm = C. Get (calendar. Minute); // minute

2:
Java. util. Date d = new java. util. Date ();
Java. Text. simpledateformat dformat = new java. Text. simpledateformat ("yyyy-mm-dd ");
String datetime = dformat. Format (d );
System. Out. println (datetime );

3:
Import java. util .*;
Import java. Text .*;

Date Now = new date ();
Dateformat defaultformat = dateformat. getdateinstance ();
Dateformat formatted format = dateformat. getdateinstance (dateformat. Short );
Dateformat mediumformat = dateformat. getdateinstance (dateformat. Medium );
Dateformat longformat = dateformat. getdateinstance (dateformat. Long );
Dateformat fullformat = dateformat. getdateinstance (dateformat. Full );
String defaultdate = defaultformat. Format (now );
String updated date = formatted format. Format (now );
String mediumdate = mediumformat. Format (now );
String longdate = longformat. Format (now );
String fulldate = fullformat. Format (now );

System. Out. println ("(default) Today:" + defaultdate );
System. Out. println ("(short) Today:" + latest date );
System. Out. println ("(medium) Today:" + mediumdate );
System. Out. println ("(long) Today:" + longdate );
System. Out. println ("(full) Today:" + fulldate );
4.
<%
Java. util. Date Now = new java. util. Date ();
Out. println (now );
%>
You can use substring () to intercept data.
Or use this string time = new simpledateformat ("yyyy-mm-dd"). Format (calendar. getinstance (). gettime );

5.
Date currenttime = new date ();
Simpledateformat formatter = new simpledateformat ("yyyy-mm-dd ");
String datestring = formatter. Format (currenttime );
The datastring format is.

Related Article

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.