import java.util.Date; import java.text.SimpleDateFormat; public class testCalendar {public static void main (String [] args) {SimpleDateFormat s = new SimpleDateFormat ( "yyyy / mm / dd hh: mm: ss"); Date C = new Date (); string d = s.format (c); system.out.println (d);} ********************** **************************** RE: give you a date function, how to use it, how to use it: 2004-6- 21 10:38:13 import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java Date type and string type Mutual conversion * / public class dateutil {/ ** * base iso 8601 date format YYYYMMDD IE, 20021225 For the 25th day of december in the year 2002 * / public static final String ISO_DATE_FORMAT = "yyyyMMdd"; / ** * Expanded ISO 8601 Date format yyyy-MM-dd ie, 2002-12-25 for the 25th day of December in the year 2002 * / public static final String ISO_EXPANDED_DATE_FORMAT = "yyyy- MM-DD "; / ** * YYYY-MM-D DHH: mm: ss * / public static string datetime_pattern = "YYYY-MM-DD HH: mm: SS"; / ** * default lenient setting for getdate. * / private static boolean lenient_date = false; / ** * temporary No * @Param JD * @return * / protected static final float normalizedjulian (float jd) {float f = math.Round (JD 0.5F) - 0.5F; returnif;} / ** * floating point value conversion to date Format
* Temporary No * Returns The date from a julian. The Julian Date Will Be Converted To Noon Gmt, * Such That It Matches The Nearest Half-Integer (IE, A Julian Date Of 1.4 gets * Changed to 1.5, And 0.9 Gets Changd to 0.5.) * * @
param JD the Julian date * @return the Gregorian date * / public static final Date toDate (float JD) {/ * To convert a Julian Day Number to a Gregorian date, assume that it is for 0 hours, Greenwich time (so * that it ends in 0.5) Do the following calculations, again dropping the fractional part of all * multiplicatons and divisions Note:.. This method will not give dates accurately on the * Gregorian proleptic Calendar, ie, the calendar you get by extending the Gregorian * Calendar Backwards to Years Earlier Than 1582. USING The Gregorian Leap Year * Rules. in particular, The Method Fails if Y <400. * / float z = (Normalizedjulian (JD)) 0.5F; Float W = (int) Z - 1867216.25F) / 36524.25F); float x = (int); float a = z 1 w - x; float b = a 1524; float c = (int) ((b - 122.1) / 365.25); Float D = (int) (365.25F * c); float e = (int) ((B - d) / 30.6001); Float f = (int) (30.6001F * e); int day = (int) (B - D - f); int month = (int) (E - 1); if (Month> 12) {Month = Month - 12;} int year = (int) (IF Month Is January or February) OR C-4716 (OtherWise) IF (Month> 2) {year--;} Calendar C = Calendar.GetInstance (); Cales (Calendar.year, Year); C.SET (Calendar.Month, Month - 1); // Damn 0 Offsets C.SET (Calendar.date, Day); Return C.getTime ();
} / ** * Returns the days between two dates. Positive values indicate that * the second date is after the first, and negative values indicate, well, * the opposite. Relying on specific times is problematic. * * @Param early the " First Date "* @Param Late the" Second Date "* @Return The days Between the Two dates * / public static final int daysbetween (Date Early, Date Late) {Calendar C1 = Calendar.GetInstance (); Calendar C2 = Calendar. GetInstance (); c1.settime (early); C2.Settime (Late); Return DaysbetWeen (C1, C2);} / ** * returns the days Between Two dates. Positive Values Indicate That * The Second Date Is After The First , and negative values indicate, well, * the opposite. * * @param early * @param late * @return the days between two dates. * / public static final int daysBetween (Calendar early, Calendar late) {return (int) ( Tojulian (Late) - Tojulian (Early); } / ** * Return a julian date based on the input parameter. This is * based from Calculation Found At * Julian Day Calculations * (Gregorian Calendar) , provided by Bill Jeffrys. * @param ca calendar instance * @return the julian day number * / public static final float toJulian (Calendar c) {int Y = c.get (Calendar. Year); int m = c.Get (Calendar.Mont); int D = c.get (Calendar.date); int A = y / 100; int b = a / 4; int C = 2 - a b; Float E = (int) (365.25F * (Y 4716));
Float f = (int) (30.6001F * (m 1)); float jd = C D E F - 1524.5F; Return JD;} / ** * Temporary No * Return A Julian Date Based on the Input Parameter. this is * based from Calculation Found at * Julian Day Calculation * (Gregorian Calendar) , provided by Bill JeffRys. * @Param Date * @return the Julian Day Number * / Public Static Final Float Tojulian (Date Date) {Calendar C = Calendar.GetInstance (); C.SetTime (Date); Return Tojulian (c);} / ** * Date Add * @Param Isostring Date String * @Param FMT Format * @Param Field Yue / Month / Day Calendar.year / Calendar.month / Calendar.date * @Param Amount Add Quantity * @Return * @Throws Parsexception * / Public static final string DateIncrease (String isostring, string fmt, int field, int amount) {Try {Calendar Cal = Gregoriancalendar.GetInstance (Timezone.getTimezon e ("gmt")); Cal.SetTime (StringTodate (isostring, fmt, true)); Cal.Add (Field, Amount); Return DateTString (Cal.getTime (), FMT);} catch (Exception EX) { Return null;}} / ** * Time Field Rolling Function. * Rolls (Up / Down) a Single Unit of Time on the given time field. * @Param isostring * @Param Field the time field. * @Param Up indeicates If rolling up or rolling down the field value. * @Param expanded use formating char's * @
. Exception ParseException if an unknown field value is given * / public static final String roll (String isoString, String fmt, int field, boolean up) throws ParseException {Calendar cal = GregorianCalendar.getInstance (TimeZone.getTimeZone ( "GMT")); Cal.settime (StringTodate (isSswate (FMT)); Cal.Roll (Field, Up); Return DateTString (Cal.getTime (), FMT);} / ** * Time Field Rolling Function. * Rolls (UP / DOWN) a single unit of time on the given time field. * * @param isoString * @param field the time field. * @param up Indicates if rolling up or rolling down the field value. * @exception ParseException if an unknown field value is given . * / Public static final string roll (String isostring, int field, boolean up) throws parseException {return roll (isostring, datetime_pattern, field, up);} / ** * string conversion to date java.util.date * @ PARAM DATEXT string * @Param Format date format * @Param lenient bounds date mark * @return * / public static Date stringToDate (String dateText, String format, boolean lenient) {if (dateText == null) {return null;} DateFormat df = null; try {if (format = = null) {df = new simpledateformat ();} else {df = new simpledateformat (format);} // setlenient avoids allowing dates limited 9/32/2001 // Which Would Otherwise Parse to 10/2/2001 DF.Setlenient (false); return df.parse (datext);
} Caturn null;}} / ** * String conversion to date java.util.date * @Param DateText string * @Param format date format * @return * / public static date stringtodate (String DateString , string format) {return stringToDate (dateString, format, LENIENT_DATE);} / ** * string into the date java.util.Date * @param dateText string * / public static date stringToDate (string dateString) {return stringToDate ( DateString, ISO_EXPANDED_DATE_FORMAT, LENIENT_DATE);} / ** Returns Time Strings According to Time Variables * @Return Returns Time Strings * @Param Pattern Time Strike Pattern * @Param Date Time Variable * / Public Static String DateTostring (Date Date, String Pattern) {if (Date == Null) {Return Null;} try {simpledateformat sfdate = new simpledateformat (pattern); sfdate.setlenient (false); return sfdate.format (date);} catch (Exception E) { Return null;}} / ** * Returns the time string YYYY-mm-dd * @Param Date * @return * / public static string DateTookString (Date, ISO_EXPANDED_DATE_FORMAT);} / * * Returns the current time * @return Returns the current time * / public static date getcurrentdatetime () {java.util.calendar Calnow = java.util.calendar.GetInstance (); java.util.date dtnow = CALNOW.GETIME (); return DTNOW;} / ** * Returns the current date string * @Param Pattern date string pattern * @return * / public static string getCurrentDateString (String Pattern) {Return DateTString (getCurrentDateTime (), Pattern);
} / ** * returns the current date string yyyy-MM-dd * @return * / public static String getCurrentDateString () {return dateToString (getCurrentDateTime (), ISO_EXPANDED_DATE_FORMAT);} / ** * returns the current date time string yyyy -Mm-dd hh: mm: ss * @Param Date * @return * / public static string DateTStringWithTime (Date Date) {Return DateTString (Date, DateTime_pattern);} / ** * Date Add - Add * @Param Date * @param days * @return java.util.Date * / public static Date dateIncreaseByDay (Date date, int days) {Calendar cal = GregorianCalendar.getInstance (TimeZone.getTimeZone ( "GMT")); cal.setTime (date); Cal.Add (Calendar.date, Days); Return Cal.gettime ();} / ** * Date Add - Add * @Param Date * @Param Days * @Return Java.util.date * / Public Static Date DateIncreasebyMonth (Date Date, Int MNT) {Calendar Cal = Gregoriancalendar.GetInstance (Timezone.gettimezone ("GMT")); Cal.SetTime (Date); Cal.Add (Calendar.Mont, MNT); Return Cal.gettime ();} / ** * Date Add - Added * @Param Date * @Param MNT * @Return Java.util .Date * / public static date DateIncreaseByyear (Date Date, Int MNT) {Calendar Cal = Gregoriancalendar.GetImezone ("GMT")); Cal.SetTime (Date); Cal.Add (Calendar.Year, MNT) Return Cal.gettime ();} / ** * Date Add * @Param Date Date String YYYY-MM-DD * @Param Days * @