JAVA common time Class and Oracle time format __java

Source: Internet
Author: User
Tags getmessage

To_char (t.datelastupdated, ' yyyy-mm-dd hh24:mi:ss:ff3 ')
, To_char (Systimestamp, ' Yyyy-mm-dd HH24:MI:SS. FF3 ')

The time format for Oracle. Note the difference between the date type and the timestamp type in the database

where
To_char (T.discharge_time, ' yyyy-mm-dd HH24:mi:ss ') >= ' 2007-12-08 10:00:00 '
and To_char (T.discharge_time, ' yyyy-mm-dd HH24:mi:ss ') <= ' 2007-12-24 10:30:00 '
//
T.discharge_time >= to_date (' 1899-12-30 1:00:00 ', ' yyyy-mm-dd HH24:mi:ss ')
and T.discharge_time <= to_date (' 2008-11-24 00:00:00 ', ' yyyy-mm-dd HH24:mi:ss ')

Select 1 from dba_tables t WHERE t.table_name = ' tbl_swifts_oper_info ' and t.tablespace_name= ' USERS ';

Java Common time Class

Package saptac.core.util;

Import Java.sql.Timestamp;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import Java.util.Calendar;
Import Java.util.Date;
Import Java.util.GregorianCalendar;

Import Org.apache.commons.logging.Log;
Import Org.apache.commons.logging.LogFactory;

public class Dateutil {
Protected final Static log = Logfactory.getlog (Dateutil.class);

private static String Defaultdatepattern = null;

private static String Timepattern = "hh:mm";

Public final static String Date_pattern = "Yyyy-mm-dd hh:mm:ss";
public static final String date_pattern_2 = "Yyyy-mm-dd hh:mm:ss. S ";

public static final void main (String a[]) {
Java.util.Date d= new Java.util.Date ();
Java.sql.Timestamp t = new Timestamp (D.gettime ());

String ret = converttimestamptostring (T,date_pattern);
SYSTEM.OUT.PRINTLN (ret);

Java.util.Date d2= new Java.util.Date ();
Java.sql.Date t2 = new Java.sql.Date (D2.gettime ());

  string ret2 =  convertsqldatetostring (T2, Date_pattern);
  system.out.println (Ret2);
  
  string ret3 =  convertsqldatetostring (T2, date_pattern_2);
  system.out.println (RET3);
  
  simpledateformat df = new SimpleDateFormat (date_pattern);
  string ret4 = Df.format (T2);
  
  java.sql.date t5 = new Java.sql.Date (D2.gettime ());
  java.util.date T6 = (java.util.Date) T5;
  string ret6 =  getdatetime (DATE_PATTERN,T6);
  system.out.println (RET6);
 }
 
 /**
  * Convert java.sql.Date to string
  * @param timestamp
  * @return
  */
 public static string convertsqldatetostring (Java.sql.Date Date, String pattern) {
  
  simpledateformat df = new SimpleDateFormat (pattern);
  string ret = df.format (date);

return ret;
}


/**
* Convert Java.sql.Timestamp to String
* @param timestamp
* @return
*/
public static string converttimestamptostring (Java.sql.Timestamp Timestamp, string pattern) {

SimpleDateFormat df = new SimpleDateFormat (pattern);
String ret = Df.format (timestamp);

return ret;
}

/**
* Get current time
* @return yyyy
*/
public static String Getcurryear ()
{
String ret = "";
SimpleDateFormat df = new SimpleDateFormat ("yyyy");
ret = Df.format (New Timestamp (System.currenttimemillis ()));
return ret;
}

/**
* Get current time
* @return Date
*/
public static Date Getnowdate ()
{

return new Date (System.currenttimemillis ());
}
/**
* Get current time
* @return MM
*
*/
public static String Getcurrmonth ()
{
String ret = "";
SimpleDateFormat df = new SimpleDateFormat ("MM");
ret = Df.format (New Timestamp (System.currenttimemillis ()));
return ret;
}

/**
* Get next month time
* @input YYYYMMDD
* @return Date
*
*/
public static Date Getendyearmonth (String inputdate)
{
SimpleDateFormat df = new SimpleDateFormat ("YyyyMMdd");
Calendar ca= calendar.getinstance ();

try {
Ca.settime (Df.parse (inputdate));
Ca.add (Calendar.month, 1);
Ca.add (Calendar.day_of_month,-1);
Ca.set (Calendar.hour_of_day, 23);
Ca.set (Calendar.minute, 59);
Ca.set (Calendar.second, 59);
catch (ParseException e) {
E.printstacktrace ();
}
return Ca.gettime ();
}
/**
* Get the month time
* @input YYYYMMDD
* @return Date
* @throws Exception
*
*/
public static Date Getyearmonth (String inputdate) throws Exception
{
SimpleDateFormat df = new SimpleDateFormat ("YyyyMMdd");
Calendar ca= calendar.getinstance ();

try {
Date Date=df.parse (inputdate);
Ca.set (calendar.hour_of_day, 0);
Ca.set (calendar.minute, 0);
Ca.set (Calendar.second, 0);
Ca.settime (date);
catch (Exception e) {
throw new Exception (E.getmessage ());
}
return Ca.gettime ();
}
/**
* Next month time
* @return Yyyymm
*
*/
public static String Getnextmonth ()
{
Calendar ca= calendar.getinstance ();
Ca.add (Calendar.month, 1);
SimpleDateFormat df = new SimpleDateFormat ("MM");
String ret = "";
ret = Df.format (Ca.gettime ());
return ret;
}
/**
* Get current time
* @return Yyyy-mm-dd Hh:mm:ss
*/
public static String Getcurrtime ()
{
String ret = "";
SimpleDateFormat df = new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss");
ret = Df.format (New Timestamp (System.currenttimemillis ()));
return ret;
}
/** Fixed date format
*
* @return
*/
public static synchronized String Getdatepattern () {
Defaultdatepattern = "YYYY-MM-DD";
return defaultdatepattern;
}

/**
* Fixed date + time format
* @return
*/
public static String Getdatetimepattern () {
return Dateutil.getdatepattern () + "HH:mm:ss". S ";
}

/**
* Convert time format
*
* @param adate
* Date from database as a string
* @return formatted string for the UI
*/
public static final String getDate (Date adate) {
SimpleDateFormat df = null;
String returnvalue = "";

if (adate!= null) {
DF = new SimpleDateFormat (Getdatepattern ());
returnvalue = Df.format (adate);
}

return (returnvalue);
}

/**
* Convert character Turn date
*
* @param amask
* The date pattern, the string is in
* @param strdate
* A string representation of a date
* @return a converted Date object
* @see Java.text.SimpleDateFormat
* @throws ParseException
*/
public static final Date convertstringtodate (string amask, String strdate)
Throws ParseException {
SimpleDateFormat df = null;
Date date = null;
DF = new SimpleDateFormat (amask);

if (log.isdebugenabled ()) {
Log.debug ("Converting '" + strdate + "' To date with mask '"
+ Amask + "'");
}

try {
Date = Df.parse (strdate);
catch (ParseException PE) {
throw new ParseException (Pe.getmessage (), Pe.geterroroffset ());
}

return (date);
}

/**
* Convert time format
* @param thetime
* The current time
* @return The current date/time
*/
public static String Gettimenow (Date thetime) {
Return GetDateTime (Timepattern, thetime);
}

/**
* @return The current date
* @throws ParseException
*/
public static Calendar Gettoday () throws ParseException {
Date today = new Date ();
SimpleDateFormat df = new SimpleDateFormat (Getdatepattern ());
String todayasstring = Df.format (today);
Calendar cal = new GregorianCalendar ();
Cal.settime (Convertstringtodate (todayasstring));

Return cal;
}

/**
* Conversion Time
* @param amask
* The date pattern, the string is in
* @param adate
* A Date Object
* @return A formatted string representation of the date
*
* @see Java.text.SimpleDateFormat
*/
public static final String GetDateTime (string amask, Date adate) {
SimpleDateFormat df = null;
String returnvalue = "";

if (adate = = null) {
Log.error ("Adate is null!");
} else {
DF = new SimpleDateFormat (amask);
returnvalue = Df.format (adate);
}

return (returnvalue);
}

/**
*
* @param adate
* A date to convert
* @return A string representation of the date
*/
public static final String convertdatetostring (Date adate) {
Return GetDateTime (Getdatepattern (), adate);
}

/**
* Character Conversion time
* @param strdate
* The date to convert (in format mm/dd/yyyy)
* @return A Date Object
*
* @throws ParseException
*/
public static Date convertstringtodate (String strdate)
{
Date adate = null;

try {
if (log.isdebugenabled ()) {
Log.debug ("Converting date with pattern:" + getdatepattern ());
}

Adate = Convertstringtodate (Getdatepattern (), strdate);
catch (ParseException PE) {
Log.error ("Could not convert '" + strdate
+ "' to a date, throwing exception");
Pe.printstacktrace ();

}

  return adate;
 }
 /**
  * To convert a string to the required format
  * @param strdate format is YYYYMMDD string
  * @return Str String
  */
    public static Timestamp converstringtossneeddate (string, formatted as YYYY-MM-DD) strdate)
    { 
     timestamp date=null;
     string str = null;
     if (strdate.length () ==8) {
      str = strdate.substring ( 0,4) + "-" +strdate.substring (4,6) + "-" +strdate.substring (6,8) + "00:00:00";
         date=timestamp.valueof (str);
     }
  else{
   str= "2006-12-26 00:00:00";
   date=timestamp.valueof (str);
  }
     return Date;
   }
   
 
}

Related Article

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.