Several ways to get the current time from Java
I. Get the current system time and date and format the output:
Import Java.util.Date;
Import Java.text.SimpleDateFormat;
public class Nowstring {
public static void Main (string[] args) {
SimpleDateFormat df = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");//Set Date format
System.out.println (Df.format (new date));//new Date () to get current system time
}
}
Two. The date in the database is output only in the year-month-day mode, and can be used in the following two ways:
1, convert () conversion function:
String sqlst = "SELECT CONVERT (varchar (), bookdate,126) as convertbookdate from Roombook where bookdate between ' 2007-4-1 0 ' and ' 2007-4-25 ';
System.out.println (rs.getstring ("convertbookdate"));
2, the use of SimpleDateFormat class:
To enter two Java packages first:
Import Java.util.Date;
Import Java.text.SimpleDateFormat;
And then:
Define date format: SimpleDateFormat SDF = new SimpleDateFormat (YY-MM-DD);
The SQL statement is: String sqlstr = "Select Bookdate from Roombook where bookdate between ' 2007-4-10 ' and ' 2007-4-25 '";
Output:
System.out.println (Df.format) (Rs.getdate ("bookdate"));
Methods for obtaining the current date and time in Java
Import Java.util.Date;
Import Java.util.Calendar;
Import Java.text.SimpleDateFormat;
public class testdate{
public static void Main (string[] args) {
Date now = new Date ();
SimpleDateFormat DateFormat = new SimpleDateFormat ("Yyyy/mm/dd HH:mm:ss");//You can easily modify the date format
String hehe = Dateformat.format (now);
SYSTEM.OUT.PRINTLN (hehe);
Calendar C = calendar.getinstance ()//Can be modified individually for each time field
int year = C.get (calendar.year);
int month = C.get (calendar.month);
int date = C.get (calendar.date);
int hour = C.get (Calendar.hour_of_day);
int minute = C.get (Calendar.minute);
int second = C.get (Calendar.second);
SYSTEM.OUT.PRINTLN (Year + "/" + month + "/" + Date + "" +hour + ":" +minute + ":" + second);
}
}
Sometimes you convert a string of time to a date type, and you can convert the time string you just obtained to the date type in the following ways.
SimpleDateFormat sdf=new SimpleDateFormat ("Yyyy-mm-dd");
Java.util.Date Time=null;
try {
Time= Sdf.parse (Sdf.format (new Date));
catch (ParseException e) {
E.printstacktrace ();
}
Date-time comparisons in Java
Java.text.SimpleDateFormat Class----Time date format
Java.util.Date Class----Construction time Date
Java.util.Date class CompareTo Method----Comparison date time
Comparison of date and time in Java using Java.text.SimpleDateFormat Class and Java.util.Date class CompareTo methods, SimpleDateFormat classes are used to format time dates, format dates into formats, and CompareTo methods to compare date times. Here's a snippet of the program I've written:
String data=doc.getitemvaluestring ("Fd_ngr"); Draft time String data4=data.substring (Data.indexof ("), Data.length ()); int Panduan=data4.indexof ("-"); int Panduan1=data4.indexof ("/"); Ec_dbsy=vi_dbsy.getallentries (); Construct date format Yyyy-mm-nn SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd"); SimpleDateFormat formatter2 = new SimpleDateFormat ("mm/dd/yyyy"); if (Data4.length ()!=0) { if (panduan!=-1) { try { Date date1 = Formatter.parse (DATA4); Date Date0=new date (); Date date2 =formatter.parse ("2006-08-31"); Date date3=formatter.parse ("2006-07-01"); int Number1=date1.compareto (DATE2); int Number2=date1.compareto (DATE3); Returns 0 indicating the same time date Back 1 representing date 1> date 2 Return-1 for date 1< date 2 if (number2==1) { if (number1==-1) { SYSTEM.OUT.PRINTLN (the date format meets the requirements.) "); The document ID of the SYSTEM.OUT.PRINTLN ("date format conforming to the requirement Yyyy-mm-dd" is: "+doc.getuniversalid ()); String mpath= "\" + "\\10.52.4.4\\d$\\Program files\\websphere\\appserver\\installedapps\\oa1\\oaweb20061030_ WAR.EAR\\OAWEB20061030.WAR\\DOC_FW "; Daglimpl Daglimpl = new Daglimpl (); DAGLIMPL.ADDFW (Doc,mpath); Archive processing } } catch (ParseException e) { E.printstacktrace (); } } |
time comparison size in JavaCategory: JAVA 2012-07-05 16:41 1062 people read comments (0) Collection report
Method One:
DateFormat FMT = new SimpleDateFormat ("Yyyy-mm-dd");
String Datebegin=fmt.format (Carriercommand.getdatebegin ());
String Dateend=fmt.format (Carriercommand.getdateend ());
If you get a date format that is not ' 2008-05-22 ', you must format the date
String Datebegin = Request.getparameter ("Datebegin");
String dateend = Request.getparameter ("DateEnd");
if (Java.sql.Date.valueOf (Datebegin). After (Java.sql.Date.valueOf (dateend)) {
Start date is greater than end date
Errors.rejectvalue ("DateEnd", NULL, "Start date must be less than End date!");
}
StringBuffer timebegin = new StringBuffer ();
StringBuffer timeend = new StringBuffer ();
Timebegin.append (Request.getparameter ("Timebegin"));
Timeend.append (Request.getparameter ("Timeend"));
Timebegin.append (": 00");
Timeend.append (": 00");
if (Java.sql.Date.valueOf (Datebegin). Equals (Java.sql.Date.valueOf (dateend)))
{///start date equals end date
if (Java.sql.Time.valueOf (timebegin.tostring ()). Equals (java.sql.Time.valueOf
(Timeend.tostring ())) Same time
{
Errors.rejectvalue ("Timeend", NULL, "Start with end date Same, start time must be less than end time!");
}
if (Java.sql.Time.valueOf (timebegin.tostring ()). After (java.sql.Time.valueOf
(Timeend.tostring ())) Same time
{
Errors.rejectvalue ("Timeend", NULL, "Start with end date Same, start time must be less than end time!");
}
}
Method Two:
Import Java.text.DateFormat;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
/**
*
* @author HP
*/
public class Test {
public static void Main (String args[]) {
int i= compare_date ("1995-11-12 15:21", "1999-12-11 09:59");
System.out.println ("i==" +i);
}
public static int Compare_date (string DATE1, String DATE2) {
DateFormat df = new SimpleDateFormat ("Yyyy-mm-dd hh:mm");
try {
Date dt1 = Df.parse (DATE1);
Date DT2 = Df.parse (DATE2);
if (Dt1.gettime () > Dt2.gettime ()) {
System.out.println ("Dt1 in DT2 before");
return 1;
else if (Dt1.gettime () < Dt2.gettime ()) {
System.out.println ("Dt1 after DT2");
return-1;
} else {
return 0;
}
catch (Exception Exception) {
Exception.printstacktrace ();
}
return 0;
}
}
Method Three:
For example, it's 2004-03-26 13:31:40.
The past is: 2004-01-02 11:30:24
I am now going to get two date difference, the difference in the form of: xx days xx hours xx minutes xx seconds
Java Calculation time difference and comparison times size
For example, it's 2004-03-26 13:31:40.
The past is: 2004-01-02 11:30:24
I am now going to get two date difference, the difference in the form of: xx days xx hours xx minutes xx seconds
One:
DateFormat df = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
Try
{
Date D1 = Df.parse ("2004-03-26 13:31:40");
Date D2 = Df.parse ("2004-01-02 11:30:24");
Long diff = d1.gettime ()-d2.gettime ();
Long days = diff/(1000 * 60 * 60 * 24);
}
catch (Exception e)
{
}
Two:
SimpleDateFormat df = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
Java.util.Date now = Df.parse ("2004-03-26 13:31:40");
Java.util.Date date=df.parse ("2004-01-02 11:30:24");
Long L=now.gettime ()-date.gettime ();
Long day=l/(24*60*60*1000);
Long hour= (l/(60*60*1000)-day*24);
Long min= ((l/(60*1000))-day*24*60-hour*60);
Long s= (l/1000-day*24*60*60-hour*60*60-min*60);
System.out.println ("" +day+ "Day" +hour+ "hour" +min+ "minute" +s+ "seconds");
Three:
SimpleDateFormat dfs = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
Java.util.Date begin=dfs.parse ("2004-01-02 11:30:24");
Java.util.Date end = Dfs.parse ("2004-03-26 13:31:40");
Long between= (End.gettime ()-begin.gettime ())/1000;//divided by 1000 to convert to seconds
Long day1=between/(24*3600);
Long hour1=between% (24*3600)/3600;
Long minute1=between%3600/60;
Long second1=between%60/60;
System.out.println ("" +day1+ "Day" +hour1+ "hour" +minute1+ "minute" +second1+ "seconds");
====================================================
Java Comparison time size
String s1= "2008-01-25 09:12:09";
String s2= "2008-01-29 09:12:11";
Java.text.DateFormat df=new Java.text.SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");<