Date problems in JSP and other __JS

Source: Internet
Author: User
Tags dateformat diff getdate local time
Date issue
1. Get server-side current date:

[Copy to Clipboard] [ - ] CODE: <%@ page import= "Java.util.Date"%>
<%
Date mydate = new Date ();
%>
2, to obtain the current year, month, day:

[Copy to Clipboard] [ - ] CODE: <%@ page import= "Java.util.Date"%>
<%
Date mydate = new Date ();
int thisyear = mydate.getyear () + 1900;//thisyear = 2003
int thismonth = Mydate.getmonth () + 1;//thismonth = 5
int thisdate = Mydate.getdate ();//thisdate = 30
%>
3. Output current date by local time zone

[Copy to Clipboard] [ - ] CODE: <%@ page import= "Java.util.Date"%>
<%
Date mydate = new Date ();
Out.println (Mydate.tolocalestring ());
%>
The output results are:
2003-5-30
4, to get the database in the field name "Publish_time", type datetime value

[Copy to Clipboard] [ - ] CODE: <%@ page import= "Java.util.Date"%>
<%
... Connect database ...
ResultSet rs = ...
Date sdate = rs.getdate ("Publish_time");
%>
[Code]
5. Print the date in the specified format
[Code]
<%@ page import= "Java.util.Date"%>
<%@ page import= "Java.text.DateFormat"%>
<%
Date Dnow = new Date ();
SimpleDateFormat formatter = new SimpleDateFormat ("E yyyy". Mm.dd ' at ' hh:mm:ss a zzz ');
Out.println ("It is" + Formatter.format (dnow));
%>
The results of the output are:
It is Friday 2003.05.30 at 11:30:46 a.m. CST
(See the SimpleDateFormat class for more detailed formatting symbols)
6. Convert a string to a date

[Copy to Clipboard] [ - ] CODE: <%@ page import= "Java.util.Date"%>
<%@ page import= "Java.text.DateFormat"%>
<%
String input = "1222-11-11";
SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd");
Date t = null;
try{
t = formatter.parse (input);
Out.println (t);
}catch (ParseException e) {
Out.println ("unparseable using" + formatter);
}
%>
The output results are:
Fri Nov 00:00:00 CST 1222
7. Calculate the interval between dates

[Copy to Clipboard] [ - ] CODE: <%@ page import= "Java.util.Date"%>
<%@ page import= "Java.text.DateFormat"%>
<%
String input = "2003-05-01";
SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd");
Date D1 = null;
try{
D1 = formatter.parse (input);
}catch (ParseException e) {
Out.println ("unparseable using" + formatter);
}

Date D2 = new Date ();

Long diff = d2.gettime ()-d1.gettime ();
Out.println ("difference is" + (diff/(1000*60*60*24)) + "days.");
%>
The output results are:
Difference is.
8, date of addition and subtraction operation
Method: Using the Add () method of the Calendar class

[Copy to Clipboard] [ - ] CODE: <%@ page import= "java.util.*"%>
<%@ page import= "java.text.*"%>
<%
Calendar now = Calendar.getinstance ();
SimpleDateFormat formatter = new SimpleDateFormat ("E yyyy". Mm.dd ' at ' hh:mm:ss a zzz ');
Out.println ("It is now" + Formatter.format (Now.gettime ()));
Now.add (calendar.day_of_year,-(365*2));
Out.println ("<br>");
Out.println ("Two years ago was" + Formatter.format (Now.gettime ()));
%>
The output results are:
It is now Friday 2003.05.30 at 01:45:32 pm CST
Two years ago was Wednesday 2001.05.30 at 01:45:32 pm CST
9. Comparison Date
Method: Using Equals (), before (), after () method

[Copy to Clipboard] [ - ] CODE: <%@ page import= "java.util.*"%>
<%@ page import= "java.text.*"%>
<%
DateFormat df = new SimpleDateFormat ("Yyy-mm-dd");
Date D1 = df.parse ("2000-01-01");
Date D2 = Df.parse ("1999-12-31");

String relation = null;
if (D1.equals (D2))
Relation = "The same date as";
else if (D1.before (D2))
Relation = "before";
Else
Relation = "after";
Out.println (D1 + "is" + relation + "+ D2);
%>
The output results are:
Sat 00:00:00 CST Fri Dec 00:00:00 CST 1999
10. Record the time spent on one thing
Method: Call two times System.gettimemillis () method, find the difference value

[Copy to Clipboard] [ - ] CODE: <%@ page import= "java.text.*"%>
<%
Long t0,t1;
T0 = System.currenttimemillis ();
Out.println ("Cyc starts at" + t0);
int k = 0;
for (int i =0;i<100000;i++) {
K = i;
}
T1 = System.currenttimemillis ();
Out.println ("<br>");
Out.println ("Cyc ends at" + T1);
Out.println ("<br>");
Out.println ("This run took" + (T1-T0) + "Ms.");
%>
The output results are:
CYC starts at 1054275312432
Cyc ends at 1054275312442
This run took 10ms.

Other: How to format decimal

[Copy to Clipboard] [ - ] CODE: <%@ page import= "java.text.*"%>
<%
DecimalFormat df = new DecimalFormat (", ###.00");
Double anumber = 33665448856.6568975;
String result = Df.format (anumber);
OUT.PRINTLN (result);
%>
The output results are:
33,665,448,856.66
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.