Time Date Base operation

Source: Internet
Author: User
Tags date now date1 time and date

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 the current system time
}
}

Two. The date in the database is only output in the form of year-month-day, which can be used in two ways:

1. CONVERT () Convert 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 getting 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 domain


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 got to date type in the following way.

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 ();
}

Java date subtraction get days

public class Dat {

static simpledateformat H;

public static void Test (date date, int m) {
H = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
String n = h.format (date);

String myString = dateformat.getdateinstance (). Format (date);
System.out.println ("After formatting:" + myString);

Timestamp time = timestamp.valueof (n);

//Days plus (minus) days
long L = time.gettime () + * * * m * +;
Time.settime (l);
System.out.println ("Calculated date:" + time);
}
public static void Main (String arg[]) {
Date date=new date (108,04,02);
Test (date,5);

}

}

Date date1 = new SimpleDateFormat ("Yyyy-mm-dd"). Parse ("2006-06-08");

Date date2 = new SimpleDateFormat ("Yyyy-mm-dd"). Parse ("2006-06-12");

Long L = date1.gettime ()-date2.gettime () >0? Date1.gettime ()-date2.gettime ():

Date2.gettime ()-date1.gettime ();

System.out.println (l/1000+ "s");

Date subtraction to get a difference

Long day = (Date1.gettime ()-date2.gettime ())/(24*60*60*1000) >0? (Date1.gettime ()-date2.gettime ())/(24*60*60*1000):

(Date2.gettime ()-date1.gettime ())/(24*60*60*1000);

System.out.println ("Date of Difference:" +day);

Time Date Base operation

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.