Java Displays the current time

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

Gets the current time in Java for the current system time in Java. 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);
}
}



public class Testgettime
{
public static String getdatetimestring ()
{
Java.text.SimpleDateFormat
SimpleDateFormat = new Java.text.SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
Return Simpledateformat.format (New Java.util.Date (System.currenttimemillis ()));
}
public static void Main (string[] args)
{
System.out.println (Getdatetimestring ());
}

}

Gregoriancalender (Standard calendar Class)

Gregoriancalender (int year,int month,int datofmonth)
Gregoriancalender (int year,int month,int datofmonth,int hours,int minutes)
Gregoriancalender (int year,int month,int datofmonth,int hours,int minutes,int seconds)

In three forms, the day, month and year are set. Year specifies the number of years since 1900. Month is specified, with 0 representing January. The day of the month is specified by DayOfMonth. The first type sets the time at midnight. The second form is set in hours and minutes, and the third form adds seconds

Calender (Calendar Class)
Import Java.util.Calendar;
Class Calendardemo
{
public static void Main (string[] args)
{
String months[] = {"Jan", "Feb", "Mar", "Apr", "May", "June", "Jul", "", "Sep", "Oct", "Nov", "Dec"};
Calendar calendar = Calendar.getinstance ();
System.out.print ("Date:");
System.out.print (Months[calendar.get (calendar.month));
System.out.print ("+calendar.get (calendar.date) +");
System.out.println (Calendar.get (calendar.year));

System.out.print ("Time:");
System.out.print (Calendar.get (Calendar.hour) + ":");
System.out.print (Calendar.get (Calendar.minute) + ":");
System.out.println (Calendar.get (Calendar.second));

System.out.print ("Updated Time:");
System.out.print (Calendar.get (Calendar.hour) + ":");
System.out.print (Calendar.get (Calendar.minute) + ":");
System.out.println (Calendar.get (Calendar.second));
}
}


Here's how to make an implementation, using Java.text.SimpleDateFormat.
And the test sample is included:
Import Java.text.SimpleDateFormat;
Import java.text.ParseException;
Import Java.util.Date;
Public final class Dateutils {
private static final SimpleDateFormat Year_format = new SimpleDateFormat ("yyyy");
private static final SimpleDateFormat Month_format = new SimpleDateFormat ("MM");
private static final SimpleDateFormat Day_format = new SimpleDateFormat ("DD");
private static final SimpleDateFormat Hour_format = new SimpleDateFormat ("HH");
private static final SimpleDateFormat Minute_format = new SimpleDateFormat ("MM");
private static final SimpleDateFormat Second_format = new SimpleDateFormat ("SS");

public static String formatyear (date date) {
return Date==null?null:year_format.format (date);
}

public static String Formatmonth (date date) {
return Date==null?null:month_format.format (date);
}

public static String Formatday (date date) {
return Date==null?null:day_format.format (date);
}

public static String Formathour (date date) {
return Date==null?null:hour_format.format (date);
}

public static String Formatminute (date date) {
return Date==null?null:minute_format.format (date);
}

public static String Formatsecond (date date) {
return Date==null?null:second_format.format (date);
}

For unit test below
private static final SimpleDateFormat Normal_format = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
Static Date Parse (String datestring) throws ParseException {
return datestring==null| | Datestring.length () ==0?null:normal_format.parse (datestring);
}
}
-----------------------------------------------
Unit tests:
Import Junit.framework.TestCase;
Import Java.util.Date;
public class Testdateutils extends TestCase {
Public testdateutils (String name) {super (name);}

public void Testformatdate () throws Exception {
String testdate = "2003-03-12 15:34:54";
Date d = dateutils.parse (testdate);
Assertnotnull (d);

Assertequals ("2003", Dateutils.formatyear (d));
Assertequals ("Dateutils.formatmonth", (d));
Assertequals ("n", Dateutils.formatday (d));
Assertequals ("C", Dateutils.formathour (d));
Assertequals ("Dateutils.formatminute", (d));
Assertequals ("WU", Dateutils.formatsecond (d));
}
}

System.out.println ("ERA:" + calendar.get (Calendar.era));
System.out.println ("Year:" + Calendar.get (calendar.year));
System.out.println ("MONTH:" + calendar.get (calendar.month));
System.out.println ("week_of_year:" + calendar.get (calendar.week_of_year));
System.out.println ("Week_of_month:" + calendar.get (calendar.week_of_month));
System.out.println ("DATE:" + calendar.get (calendar.date));
System.out.println ("Day_of_month:" + calendar.get (calendar.day_of_month));
System.out.println ("day_of_year:" + calendar.get (calendar.day_of_year));
System.out.println ("Day_of_week:" + calendar.get (Calendar.day_of_week));
System.out.println ("Day_of_week_in_month:"
+ calendar.get (calendar.day_of_week_in_month));
System.out.println ("am_pm:" + calendar.get (calendar.am_pm));
System.out.println ("HOUR:" + calendar.get (calendar.hour));
System.out.println ("Hour_of_day:" + calendar.get (calendar.hour_of_day));
System.out.println ("MINUTE:" + calendar.get (Calendar.minute));
System.out.println ("SECOND:" + calendar.get (Calendar.second));
System.out.println ("Millisecond:" + calendar.get (Calendar.millisecond));
System.out.println ("Zone_offset:"
+ (Calendar.get (calendar.zone_offset)/(60*60*1000)));
System.out.println ("Dst_offset:"
+ (Calendar.get (calendar.dst_offset)/(60*60*1000)));
System.out.println ("Current time, with hour reset to 3");
Calendar.clear (Calendar.hour_of_day); So doesn ' t override
Calendar.set (Calendar.hour, 3);


Ways to get the current time in Java
There are two ways of doing this:

Method one: With the Java.util.Date class to achieve, and combined with the Java.text.DateFormat class to achieve the format of time, see the following code:

import java.util.*;
Import java.text.*;
//The following default time and date display is in Chinese language mode
//General language default Chinese is available, the format of the time date is medium style, such as: 2008-6-16 20:54:53
// The dates and times shown below are based on the date class, and can be implemented using the Calendar class Testdate2.java
public class TestDate {
public static void main ( String[] (args) {
Date now = new Date ();
Calendar cal = Calendar.getinstance ();

DateFormat d1 = dateformat.getdateinstance ();//default style (medium style, for example: 2008-6-16 20:54:53) in default language (Chinese)
String STR1 = D1.format (now);
DateFormat d2 = Dateformat.getdatetimeinstance ();
String str2 = D2.format (now);
DateFormat d3 = Dateformat.gettimeinstance ();
String STR3 = D3.format (now);
DateFormat d4 = dateformat.getinstance ();//use short style to display the date and time
String STR4 = D4.format (now);

DateFormat d5 = Dateformat.getdatetimeinstance (dateformat.full,dateformat.full); Display date, week, time (accurate to seconds)
String STR5 = D5.format (now);
DateFormat d6 = dateformat.getdatetimeinstance (Dateformat.long,dateformat.long); Displays the date. Time (accurate to seconds)
String STR6 = D6.format (now);
DateFormat D7 = dateformat.getdatetimeinstance (Dateformat.short,dateformat.short); Display date, time (accurate to minute)
String STR7 = D7.format (now);
DateFormat D8 = Dateformat.getdatetimeinstance (Dateformat.medium,dateformat.medium); Display date, time (accurate to minute)
String str8 = D8.format (now);//compared with short style, this method is the most useful


System.out.println ("Show Time in Date:" + now);//The result shown by this method is the same as Calendar.getinstance (). GetTime ().


SYSTEM.OUT.PRINTLN ("formatted with Dateformat.getdateinstance () after the time:" + str1);





2 ways to get the current time in Java
SYSTEM.OUT.PRINTLN ("formatted with Dateformat.getdatetimeinstance () after the time:" + str2);
SYSTEM.OUT.PRINTLN ("formatted with Dateformat.gettimeinstance () after the time:" + STR3);
SYSTEM.OUT.PRINTLN ("formatted with Dateformat.getinstance () after the time:" + STR4);

SYSTEM.OUT.PRINTLN ("Formatted time with Dateformat.getdatetimeinstance (dateformat.full,dateformat.full):" + STR5);
SYSTEM.OUT.PRINTLN ("Formatted time with Dateformat.getdatetimeinstance (Dateformat.long,dateformat.long):" + STR6);
SYSTEM.OUT.PRINTLN ("Formatted time with Dateformat.getdatetimeinstance (Dateformat.short,dateformat.short):" + STR7);
SYSTEM.OUT.PRINTLN ("Formatted time with Dateformat.getdatetimeinstance (Dateformat.medium,dateformat.medium):" + str8);
}

}

Operation Result:

Display time in Date: Mon June 20:54:53 CST 2008
Format time with Dateformat.getdateinstance (): 2008-6-16
Format time with Dateformat.getdatetimeinstance (): 2008-6-16 20:54:53
After formatting the time with Dateformat.gettimeinstance (): 20:54:53
After formatting the time with Dateformat.getinstance (): 08-6-16 8:54
After the time is formatted with dateformat.getdatetimeinstance (Dateformat.full,dateformat.full)
: June 16, 2008 Monday 08:54 P.M. 53 sec CST
After the time is formatted with dateformat.getdatetimeinstance (Dateformat.long,dateformat.long)
: June 16, 2008 08:54 P.M. 53 seconds
After you format the time with Dateformat.getdatetimeinstance (Dateformat.short,dateformat.short)
is: 08-6-16 8:54
Format time with dateformat.getdatetimeinstance (Dateformat.medium,dateformat.medium)
After: 2008-6-16-20:54:53


Method Two: Use the Java.util.Calendar class to achieve, see below:

Import java.util.*;
Import java.text.*;
The following is the use of the Calendar class to implement date time, and the date class is relatively simple

public class TestDate2 {
public static void Main (string[] args) {

Calendar CA = Calendar.getinstance ();
int year = Ca.get (calendar.year);//Get Years
int Month=ca.get (calendar.month);//Get month
int Day=ca.get (calendar.date);//Acquisition Day
int Minute=ca.get (calendar.minute);//min
int Hour=ca.get (calendar.hour);//hour
int Second=ca.get (calendar.second);//sec
int weekofyear = Ca.get (Calendar.day_of_week);


System.out.println ("with Calendar.getinstance (). GetTime () mode display time:" + ca.gettime ());
System.out.println ("Get Date with calendar:" + year + "years" + month + "Month" + Day + "Days");

SYSTEM.OUT.PRINTLN ("Use calendar to get time is:" + hour + "when" + Minute + "min" + second + "seconds");
System.out.println (weekofyear);//show today is the day of the week (I do this example is exactly Tuesday, so the results show 2, if you run again 6 weeks, then show 6)

}

}
The operating result is:
Display time by Calendar.getinstance (). GetTime (): Mon June 21:54:21 CST 2008
Date obtained with calendar is: May 16, 2008
Use calendar to get the time: 9:54 21 seconds
2


Summary: In the case, the second method is the most convenient, the method of a marked score clumsy, but to see the individual like.

There is also a way to use System.currenttimemillis () can also, the next time to summarize this method.

Java Displays the current time

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.