A handy date-time Class (Java)

Source: Internet
Author: User

Package util;

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

/**
* A <code> DateTime </code> defines some convenient formatting operations for date and time
*
* @version 1.0
* @author Markhuang
*/
public class DateTime {
/**
* Store time and date, default current time and date
*/
Private Calendar cale = Calendar.getinstance ();
Private Calendar cale = new GregorianCalendar ();

/**
* Default constructor, get current time and date
*/
Public DateTime () {
Cale.setlenient (FALSE);
}

/**
* constructor, can set milliseconds
*
* @param millisecond
* The number of milliseconds in the past
* @see #DateTime
*/
Public DateTime (long millisecond) {
Cale.setlenient (FALSE);
Cale.settimeinmillis (millisecond);
}

/**
* constructor to set the date and time of year
*
* @param year
* Year
* @param month
* Month
* @param Day
* Day
* @param hour
* Hours
* @param minute
* Minutes
* @param second
* Seconds
* @see #DateTime (Long)
*/
Public DateTime (int., int month, int day, int hour, int minute,
int second) {
Cale.setlenient (FALSE);
Cale.set (Calendar.year, year);
The foreigner's month is from 0 to 11
Cale.set (Calendar.month, month-1);
Cale.set (Calendar.day_of_month, day);
Cale.set (Calendar.hour_of_day, HOUR);
Cale.set (Calendar.minute, MINUTE);
Cale.set (Calendar.second, SECOND);
}

/**
* Get when the year before last
*
* @return when the year before
*/
public int year () {
Return Cale.get (calendar.year);
}

/**
* Get the current month
*
* @return Current Month
*/
public int month () {
The foreigner's month is from 0 to 11
Return Cale.get (Calendar.month) + 1;
}

/**
* Get the current day
*
* @return Current day
*/
public int Day () {
Return Cale.get (Calendar.day_of_month);
}

/**
* Get the current hour
*
* @return Current Hour
*/
public int hour () {
Return Cale.get (Calendar.hour_of_day);
}

/**
* Get the current minute
*
* @return Current minute
*/
public int minute () {
Return Cale.get (Calendar.minute);
}

/**
* Get current seconds
*
* @return Current seconds
*/
public int second () {
Return Cale.get (Calendar.second);
}

/**
* Get current millisecond
*
* @return Current millisecond
*/
public int millisecond () {
Return Cale.get (Calendar.millisecond);
}

/**
* Get the total number of milliseconds
*
* @return Total number of milliseconds
*/
Public long Allmillisecond () {
return Cale.gettimeinmillis ();
}

/**
* Get the current day of the week
*
* @return Current day of the week
*/
public int DayOfWeek () {
Foreigners Sunday is the first day of the week
int dayofweek = Cale.get (Calendar.day_of_week);
return DayOfWeek = = 1? 7:dayofweek-1;
}

/**
* Get the current day of the week
*
* @return Current day of the week
*/
Public String DAYOFWEEK_CN () throws ArrayIndexOutOfBoundsException {
Foreigners Sunday is the first day of the week
String[] DayOfWeek = {"Day", "one", "two", "three", "four", "five", "six"};
Return "Week" + dayofweek[cale.get (Calendar.day_of_week)-1];
}

/**
* Get current is the first day of the year
*
* @return The first day of the year
*/
public int dayofyear () {
Return Cale.get (calendar.day_of_year);
}

 /**
  * Get YYYYMMDDHHMMSS format Date
  *
  * @return YYYYMMDDHHMMSS format Date
  * @see #lo Ngdatetime (char, char)
  */
 public String shortdatetime () throws ArrayIndexOutOfBoundsException {
  simpledateformat format = null;
  try {
   format = new SimpleDateFormat ("Yyyymmddhhmmss");
&NBSP;&NBSP} catch (IllegalArgumentException e) {
   throw new arrayindexoutofboundsexception ();
&NBSP;&NBSP}
  return Format.format (Cale.gettime ());
 .}

 /**
  * Get a date similar to YYYY-MM-DD HH:MM:SS format
  *
  * @param datesep
  *  & nbsp;         Date Separator
  * @param timesep
  *             Time Separator
  * @return date similar to YYYY-MM-DD HH:MM:SS format
  * @see #longdatelongtime (char, char)
  */
 public String longdatetime (char datesep, char timesep)
   throws arrayindexoutofboundsexception {
  string pattern = "yyyy" + datesep + "MM" + D Atesep + "DD";
  pattern + + "HH" + timesep + "mm" + timesep + "ss";
  simpledateformat format = null;
  try {
   format = new SimpleDateFormat (pattern);
&NBSP;&NBSP} catch (IllegalArgumentException e) {
   throw new arrayindexoutofboundsexception ();
&NBSP;&NBSP}
  return Format.format (Cale.gettime ());
 .}

/**
* Get a date similar to YYYY-MM-DD Hh:mm:ss:SSS format
*
* @param datesep
* Date Separator symbol
* @param timesep
* Time Separator symbol
* @return A date similar to YYYY-MM-DD Hh:mm:ss:SSS format
* @see #longdatetime (char, char)
*/
Public String Longdatelongtime (Char datesep, char timesep)
Throws ArrayIndexOutOfBoundsException {
String pattern = "yyyy" + datesep + "MM" + datesep + "DD";
Pattern = + "HH" + timesep + "mm" + timesep + "ss";
Pattern + + timesep + "SSS";
SimpleDateFormat format = null;
try {
format = new SimpleDateFormat (pattern);
catch (IllegalArgumentException e) {
throw new ArrayIndexOutOfBoundsException ();
}
Return Format.format (Cale.gettime ());
}

 /**
  * Get a date similar to YYYYMMDD format
  *
  * @return A date similar to YYYYMMDD format
  * @see #longdate ( char)
  */
 public String shortdate () throws arrayindexoutofboundsexception {
   SimpleDateFormat format = null;
  try {
   format = new SimpleDateFormat ("YyyyMMdd");
&NBSP;&NBSP} catch (IllegalArgumentException e) {
   throw new arrayindexoutofboundsexception ();
&NBSP;&NBSP}
  return Format.format (Cale.gettime ());
 .}

/**
* @param datesep
* Date Separator symbol
* @return Date similar to YYYY-MM-DD format
* @see #shortdate ()
*/
Public String longdate (char datesep) throws ArrayIndexOutOfBoundsException {
String pattern = "yyyy" + datesep + "MM" + datesep + "DD";
SimpleDateFormat format = null;
try {
format = new SimpleDateFormat (pattern);
catch (IllegalArgumentException e) {
throw new ArrayIndexOutOfBoundsException ();
}
Return Format.format (Cale.gettime ());
}

/**
* @param s
* The value that is output to the output terminal
*/
Public <T> void Out (T s) {
System.out.println (s);
}

public static void Main (string[] args) {
TODO auto-generated Method Stub
try {
DateTime dt = new DateTime ();
Dt.out (Dt.shortdatetime ());
Dt.out (Dt.dayofyear ());
Dt.out (Dt.day ());
Dt.out (Dt.month ());
Dt.out (DT.DAYOFWEEK_CN ());
Dt.out (Dt.longdatetime ('-', ' & '));
Dt.out (Dt.longdatelongtime ('-', ': '));
DT = new DateTime (2009, 5, 9, 12, 56, 12);
Dt.out ("AA:" + dt.longdatelongtime ('-', ': '));
Dt.out (Dt.hour ());
Dt.out (Dt.shortdatetime ());
Dt.out (Dt.shortdate ());
Dt.out (Dt.dayofweek ());
Dt.out (Dt.dayofyear ());
Dt.out (New DateTime (). Allmillisecond ()-Dt.allmillisecond ());
DT = new DateTime (Dt.allmillisecond ()-1000000000);
Dt.out (Dt.minute ());
Dt.out (Dt.second ());
Dt.out (Dt.longdatelongtime ('-', ': '));
catch (ArrayIndexOutOfBoundsException e) {
New DateTime (). Out ("Exception:" + E.getmessage ());
catch (Java.lang.IllegalArgumentException e) {
New DateTime (). Out ("Exception:" + E.getmessage ());
}
}
}

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.