Date related code

Source: Internet
Author: User
Tags dateformat diff getdate local time

Date issue

When you change a string to a date, be aware that the format matches the line.

1. Get server-side current date:
<%@ page import= "Java.util.Date"%>
<%
Date mydate = new Date ();
%>

2, to obtain the current year, month, day:
<%@ 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
<%@ 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
<%@ page import= "Java.util.Date"%>
<%
... Connect database ...
ResultSet rs = ...
Date sdate = rs.getdate ("Publish_time");
%>
[Code]

5. Print date in specified format (date to string)
[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
<%@ 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
<%@ 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
<%@ 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
<%@ 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
<%@ 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

<%@ 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

======================

Date comparison:

The calculation and comparison of dates in Java can be resolved using date and DateFormat, and here is a sample code:

Import java.text.*;
Import java.util.*;

public class test{

public static void Main (string[] args) {
try{
Date Date=new date ();
DateFormat df=dateformat.getdatetimeinstance ();
String Now=df.format (date);
System.out.println ("Now Time:" +now);

System.out.println ("Now Time is before 16:00:" +date.before (Df.parse ("2004-12-24 16:00:00"));
}
catch (ParseException e) {System.out.print (E.getmessage ());
}
}
}

<%= ("" +equipmentbuyinfo.getbuydate ()). Equals ("null")? "":( Equipmentbuyinfo.getbuydate (). toString ()). substring (0,10)%>

(New SimpleDateFormat ("Yyyy-mm-dd hh:mm")). Format (Abean.getuploaddate ())

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.