Avatar billede c_s_p Nybegynder
16. december 2005 - 11:33 Der er 7 kommentarer og
1 løsning

Dato i java

Hej

Jeg skal bruge en funktion ligesom getCurrentDate() der bare retunerer CurrentDate + 14 dage altså en dato der ligger 14 dage frem i tiden fra det tidspunkt funktionen bliver kaldt.

Håber der er nogle der kan hjælpe

På forhånd tak.
Avatar billede kalp Novice
16. december 2005 - 11:51 #1
http://javaalmanac.com/egs/java.util/GetCurDate.html

så kan du måske sige

int day = cal.get(Calendar.DAY_OF_MONTH);      // 1...
day = day + 14;
Avatar billede mikkelbm Nybegynder
16. december 2005 - 12:06 #2
Sådan her:

Calendar cal = new GregorianCalendar ();
Date now = cal.getTime();

cal.add (Calendar.DAY_OF_MONTH, 14);
Date future = cal.getTime ();

System.out.println (now);
System.out.println (future);
Avatar billede mikkelbm Nybegynder
16. december 2005 - 12:13 #3
Og vil du den anden vej, så er det bare et negativt tal:

cal.add (Calendar.DAY_OF_MONTH, -14);


Læs evt. mere her:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Calendar.html
http://java.sun.com/j2se/1.5.0/docs/api/java/util/GregorianCalendar.html
Avatar billede Slettet bruger
16. december 2005 - 15:09 #4
Jeg har en wrapper klasse liggende til Dato, den er ret effektiv og meget nem at bruge. Smid mig en mail på bp@maximuss.dk så skal jeg se om jeg ikke kan huske at ligge den op på min server når jeg kommer hjem.
Avatar billede mikkelbm Nybegynder
16. december 2005 - 16:22 #5
Hvorfor ikke bare poste den her?
Avatar billede mikkelbm Nybegynder
16. december 2005 - 16:25 #6
Jeg tvivler dog på at det kan blive meget mere effektiv end de tre linjer der skal til for at flytte datoen 14 dage :)

Calendar cal = new GregorianCalendar ();
cal.add (Calendar.DAY_OF_MONTH, 14);
Date date = cal.getTime();
Avatar billede Slettet bruger
20. december 2005 - 11:46 #7
Jeg kan godt poste den her, men den fylder en masse, men her kommer den.
(sorry, for det lidt sene svar)
Det er rigtigt at lige netop det er meget simpelt, men der er altså noget over den wrapper klasse som jeg kan godt kan lide :-)


package booking.bl.maxicom;



import java.text.*;
import java.util.*;

/**
* A simple and straightforward date/time implementation that requires no knowledge of
* java date mechanics or formatting.<br /><br />
*
* The default date format is "YYYY-MM-dd HH:mm.ss.S".  If the format is not changed by calling
* setFormat(String), any string values sent to setDate(String), or to the String based constructors, must match
* this format exactly or an exception is thrown.  An example: "1993-02-12 12:00:00.000".  If only a date is desired, leave
* the time as "0:0:0.0".
*
* @author  Josh Vanderberg
*/

public class SimpleDate implements Comparable{

/** The internal calendar used by an instance of date */
private Calendar cal = null;

/** The default format returned by toString() - simple and it sorts properly */
private String format = "yyyy-MM-dd HH:mm:ss.S";

/**
* Create a SimpleDate using a java.util.Date
*
*/ 
public SimpleDate(Date dt) {
  cal = Calendar.getInstance();
  cal.setTime(dt);
}


/**
* Create a SimpleDate using another simpledate
*
*/ 
public SimpleDate(SimpleDate dt) {
  cal = Calendar.getInstance();
  setDate(dt.getDate());
  format = new String(dt.getFormat());
}

/**
* Create a default SimpleDate instance usings the current date/time.
*
*/ 
public SimpleDate() {
  cal = Calendar.getInstance();
}

/**
* Create a SimpleDate instance using the time zone provided, and a string to be parsed as a date.
* The date string is parsed by an instance of SimpleDateFormatter.  See Sun's javadocs for it's parsing.
* If timeZone is null, the current time zone is used instead.
*
* @param      date        A string to parse as a date
* @param      timeZone    The time zone to be used by this date
* @throws    ParseException
*/ 

public SimpleDate(String date,String timeZone) throws ParseException{
 
  cal = Calendar.getInstance();
  cal.setTimeZone(TimeZone.getTimeZone(timeZone));
 
  SimpleDateFormat formatter = new SimpleDateFormat(format);
  formatter.getCalendar().setTimeZone(TimeZone.getTimeZone(timeZone));
  formatter.setLenient(true);
     
  Date dt = formatter.parse(date);
  cal.setTime(dt);

}

/**
* Create a SimpleDate instance using a string to be parsed as a date.
* The date string is parsed by an instance of SimpleDateFormatter.  See Sun's javadocs for it's parsing rules
* but in general it is a very lenient parser.  The default time zone is used.
*
* @param      date        A string to parse as a date
* @throws ParseException
*/ 

public SimpleDate(String date) throws ParseException {
 
  cal = Calendar.getInstance();
 
  SimpleDateFormat formatter = new SimpleDateFormat(format);
  formatter.setLenient(true);
 
  Date dt = formatter.parse(date);
  cal.setTime(dt);
   
}

/**
* Create a SimpleDate instance using day, month and year values in the time zone provided.
* Time is set to 12 midnight.
*
* @param      year      The year value
* @param      month      The month value
* @param      day        The day value
* @param      timeZone  The time zone as a String e.g. EST, GMT, CMT
   
*/ 

public SimpleDate(int year, int month, int day, String timeZone) {
 
  cal = Calendar.getInstance();
  cal.setTimeZone(TimeZone.getTimeZone(timeZone));
  setDay(day);
  setMonth(month);
  setYear(year);
  setHour(0);
  setMinute(0);
  setSecond(0);
  setMillisecond(0);
}

/**
* Create a SimpleDate instance using day, month and year values in the default time zone.
* Time is set to 12 midnight.
*
* @param      year      The year value
* @param      month      The month value
* @param      day        The day value
   
*/ 

public SimpleDate(int year, int month, int day) {
 
  cal = Calendar.getInstance();
  setDay(day);
  setMonth(month);
  setYear(year);
  setHour(0);
  setMinute(0);
  setSecond(0);
  setMillisecond(0);
}


/**
* Create a SimpleDate instance using day, month, year, hours, minutes, seconds, and milliseconds
* values in the time zone provided.
*
* @param      year      The year value
* @param      month      The month value
* @param      day        The day value
* @param      hour        The hour value
* @param      minute      The minute value
* @param      second      The second value
* @param      millisecond The millisecond value 
*/ 

public SimpleDate(int year, int month, int day, int hour, int minute, int second, int millisecond, String timeZone) {
 
  cal = Calendar.getInstance();
  cal.setTimeZone(TimeZone.getTimeZone(timeZone));
  setDay(day);
  setMonth(month);
  setYear(year);
  setHour(hour);
  setMinute(minute);
  setSecond(second);
  setMillisecond(millisecond);
}



/**
* Create a SimpleDate instance using day, month, year, hours, minutes, seconds, and milliseconds
* values in default time zone.
*
* @param      year        The year value
* @param      month      The month value
* @param      day        The day value
* @param      hour        The hour value
* @param      minute      The minute value
* @param      second      The second value
* @param      millisecond The millisecond value
*/ 

public SimpleDate(int year, int month, int day, int hour, int minute, int second, int millisecond) {
  cal = Calendar.getInstance();
  setDay(day);
  setMonth(month);
  setYear(year);
  setHour(hour);
  setMinute(minute);
  setSecond(second);
  setMillisecond(millisecond);
}

/**
* Get the internal Calendar used by this SimpleDate instance.
*
*/ 
public Calendar getCalendar() {
  return cal;
}


/**
* We implement Comparable - use the difference in milliseconds between two dates to determine
* ordinal position.  Returns 0 if equal.  -1 if current instance is less than the SimpleDate passed as a parameter.
* 1 if current instance is greater than the SimpleDate passed as a parameter. Throws an casting exception if parameter is not
* a SimpleDate.
*/
public int compareTo(Object o) {
  SimpleDate dt = (SimpleDate)o;
  long diff = differenceMilliseconds(dt);
  if (diff==0) return 0; 
  if (diff > 0 ) {
      return 1;
  } else {
      return -1;
  }
}
 
 
/**
*  Override equals.
*/
public boolean equals(Object o) {
  SimpleDate dt = (SimpleDate)o;
  return (compareTo(dt)==0); 
}

/**
* Set the format string used by toString().  See the javadocs for SimpleDateFormatter for the rules
* for creating a valid format string.  Note that changing the format string changes the output of toString only,
* it does not change the output of any of the other formatting methods.
* @param  format      A format string, as required by an instance of SimpleDateFormatter
*/
public void setFormat(String format) {
  this.format = format;
}


/**
* Get the format string used by toString().
* See the javadocs for SimpleDateFormatter for information on format strings.
*/
public String getFormat() {
  return format;
}


/**
* Returns a string representation of the date/time value stored in this instance of SimpleDate.
* The current format string is used.
* @return    A formatted representing the date/time value of this SimpleDate instance.
*/

public String toString() {
  return format(format);
}

/**
* Add milliseconds to this date/time value.
* @param milliseconds    the number of milliseconds to add
*/
public void addMilliseconds(int milliseconds) {
  cal.add(Calendar.MILLISECOND,milliseconds);
}

/**
* Set the millisecond value of this date/time value.
* @param millisecond 
*/
public void setMillisecond(int millisecond) {
  cal.set(Calendar.MILLISECOND,millisecond);
}

/**
* Get the millisecond value of this date/time value.
* @return  The milliseconds in the second.
*/
public int getMillisecond() {
  return cal.get(Calendar.MILLISECOND);
}

/**
* Add seconds to this date/time value.
* @param  seconds    The number of seconds to add
*/
public void addSeconds(int seconds) {
  cal.add(Calendar.SECOND,seconds);
}

/**
* Set the second value of this date/time value.
* @param second
*/
public void setSecond(int second) {
  cal.set(Calendar.SECOND,second);
}

/**
* Get the second value of this date/time value.
* @return The seconds in the minute.
*/
public int getSecond() {
  return cal.get(Calendar.SECOND);
}

/**
* Add minutes to this date/time value.
* @param minutes    the number of minutes to add.
*/
public void addMinutes(int minutes) {
  cal.add(Calendar.MINUTE,minutes);
}

/**
* Set the minute value of this date/time value.
* @param    minute
*/
public void setMinute(int minute) {
  cal.set(Calendar.MINUTE,minute);
}

/**
* Get the minute value of this date/time value.
* @return minutes in the hour.
*/
public int getMinute() {
  return cal.get(Calendar.MINUTE);
}

/**
* Add hours to this date/time value.
* @param  hours    The number of hours to add.
*/
public void addHours(int hours) {
  cal.add(Calendar.HOUR,hours);
}

/**
* Set the hour value of this date/time value.
* @param hour
*/
public void setHour(int hour) {
  cal.set(Calendar.HOUR_OF_DAY,hour);
}

/**
* Get the hour value of this date/time value.
* @return The hour in the day.
*/
public int getHour() {
  return cal.get(Calendar.HOUR_OF_DAY);
}

/**
* Add days to this date/time value.
* @param days  The number of days to add.
*/
public void addDays(int days) {
  cal.add(Calendar.DAY_OF_YEAR, days);
}

/**
* Set the hour value of this date/time value.
* @param  day    The day of the month.
*/
public void setDay(int day) {
  cal.set(Calendar.DAY_OF_MONTH, day);
}

/**
* Get the day of the month of this date/time value.
* returns Day of the month.
*/
public int getDay() {
  return cal.get(Calendar.DAY_OF_MONTH);
}

/**
* Get the day of the year.
*/
public int getDayOfYear() {
  return cal.get(Calendar.DAY_OF_YEAR);
}

/**
* Get the day of the week
*/
public int getDayOfWeek() {
  return cal.get(Calendar.DAY_OF_WEEK);
}

/**
* Add months to this date/time value
* @param months  The number of months to add.
*/
public void addMonths(int months) {
  cal.add(Calendar.MONTH, months);
}

/**
* Set the month value of this date/time value
* @param month  The month value (1-2)
*/
public void setMonth(int month) {
  //Adjust for the programmer's love of 0 indexing.  Not appropriate for dates.
  cal.set(Calendar.MONTH, month-1);
}
/**
* Get the month of this date/time value.
* @return month in the year (1-12)
*/
public int getMonth() {
  return cal.get(Calendar.MONTH)+1;
}

/**
* Add years to this date/time value.
* @param years  The number of years to add.
*/
public void addYears(int years) {
  cal.add(Calendar.YEAR, years);
}

/**
* Set the year value of this date/time value.
* @param year
*/
public void setYear(int year) {
  cal.set(Calendar.YEAR, year);
}

/**
* Get the year value of this date/time value.
* @return The year.
*/
public int getYear() {
  return cal.get(Calendar.YEAR);
}

/**
* Set the date/time value of this SimpleDate instance using a java.util.Date value.
* @param date    A java.util.Date value.
*/
public void setDate(Date date) {
  cal.setTime(date);
}

/**
* Set the date/time value of this SimpleDate instance using a String.  The String value is parsed by an instance
* of SimpleDateFormatter.
*/
public void setDate(String date) throws ParseException{
 
  SimpleDateFormat formatter = new SimpleDateFormat(format);
  formatter.setLenient(true); 
  Date dt = formatter.parse(date);
  cal.setTime(dt);
}

/**
* Return a java.util.Date representing the date/time value stored in this instance of SimpleDate.
*/
public Date getDate() {
  return cal.getTime();
}

/**
* Create a deep copy of this SimpleDate Instance
*/
public SimpleDate copy() {
  SimpleDate dt2 = new SimpleDate();
  dt2.setDate(getDate());
  dt2.setFormat(new String(getFormat()));
  return dt2; 


/**
* Return a new SimpleDate representing the first day of the month of this SimpleDate.
* @return SimpleDate
*/
public SimpleDate firstDayOfMonth() {
  SimpleDate dt = copy();
  dt.setDay(1);
  return dt;
}

/**
* Return a new SimpleDate representing the last day of the month of this SimpleDate.
* @return SimpleDate
*/
public SimpleDate lastDayOfMonth() {
  SimpleDate dt = copy();
  dt.setMonth(getMonth()+1);
  dt.setDay(0);  //weird, but it works, the zeroth day of a month is the last day of the previous month.
  return dt;
}

/**
* Set the time zone of this SimpleDate instance. Examples "GMT", "CST", "EST", "CMT"...
* @param tz  The time zone.
*/
public void setTimeZone(TimeZone tz) {
  cal.setTimeZone(tz);
}

/**
* Return the current time zone in use by this instance.
* @return TimeZone
*/
public TimeZone getTimeZone() { 
  return cal.getTimeZone();
}

/**
* Return the number of milliseconds since midnight Jan 1, 1970 GMT represented by this date/time value.
* @return long
*/
public long getTimeInMillis() {
  return cal.getTimeInMillis();
}

/**
* The difference between this date, and another date in milliseconds.
* @param  dt    The date to subtract from this date/time value.
* @return long
*/
public long differenceMilliseconds(SimpleDate dt) {
  return (getTimeInMillis() - dt.getTimeInMillis());
}

/**
* The difference between this date, and another date in seconds.
* @param  dt    The date to subtract from this date/time value.
* @return long
*/
public long differenceSeconds(SimpleDate dt) {
  return differenceMilliseconds(dt)/1000;
}

/**
* The difference between this date, and another date in minutes.
* @param  dt    The date to subtract from this date/time value.
* @return int
*/

public int differenceMinutes(SimpleDate dt) {
  return (int)(differenceMilliseconds(dt)/60000);  //difference divided by milliseconds in an minute
}

/**
* The difference between this date, and another date in hours.
* @param  dt    The date to subtract from this date/time value.
* @return int
*/

public int differenceHours(SimpleDate dt) {
  return (int)(differenceMilliseconds(dt)/3600000);  //difference divided by milliseconds in an hour
}

/**
* The difference between this date, and another date in days.
* @param  dt    The date to subtract from this date/time value.
* @return int
*/

public int differenceDays(SimpleDate dt) {
  return (int)(differenceMilliseconds(dt)/86400000);  //difference divided by milliseconds in a day
}

/**
* The difference between this date, and another date in years.
* @param  dt    The date to subtract from this date/time value.
* @return int
*/

public int differenceYears(SimpleDate dt) {
  return getYear() - dt.getYear();
}

/**
* The difference between this date, and another date in months.
* @param  dt    The date to subtract from this date/time value.
* @return int
*/

public int differenceMonths(SimpleDate dt) {
  return 12*(getYear() - dt.getYear())+getMonth()-dt.getMonth();
}

/**
* Get the Julian date represent by this date/time value.
* The Julian date is the number of days that have elapsed since noon Universal Time on January 1, 4713 BCE.  Fractional
* components of a day are included in decimal format.
* @return double
*/

public double getJulianDate() {
  SimpleDate dt= new SimpleDate(-4712,1,1,12,0,0,0,"GMT");
  long diffMillis = differenceMilliseconds(dt);
  return ((double)diffMillis)/86400000d;
}


/**
* Formats this date/time value using the format string provided.
* @param format  A format string used by an instance of SimpleDateFormatter to create a String representation of this date/time value.
* @return String - A string representation of this date/time value.
*/
private String format(String format) {
  SimpleDateFormat formatter = new SimpleDateFormat(format);
  formatter.getCalendar().setTimeZone(getTimeZone());
  return formatter.format(new java.util.Date(cal.getTimeInMillis()));
}

/**
* Formats this date/time value using a short US format - 12/01/2003 for example.
* @return String - A string representation of this date/time value.
*/

public String shortFormatUS() {
  return format("MM/dd/YYYY");
}

/**
* Formats this date/time value using a short European format - 23/01/2003 for example.
* @return String - A string representation of this date/time value.
*/

public String shortFormatEU() {
  return format("dd/MM/yyyy");
}

/**
* Formats this date/time value using the http date format.
* @return String - A string representation of this date/time value.
*/

public String httpFormat() {
  return format("EEE, dd MMM yyyy HH:mm:ss z");
}

/**
* Formats this date/time value using a formal format.  For example - "Saturday, January 1, 2003"
* @return String - A string representation of this date/time value.
*/

public String formalFormat() {
  return format("EEEE, MMMM d, yyyy");


/**
* Formats this date/time value using a formal format including the time.  For example - "Saturday, January 1, 2003 - 12:01AM"
* @return String - A string representation of this date/time value.
*/

public String formalwithtimeFormat() {
  return format("EEEE, MMMM d, yyyy - h:mma");


/**
* Formats this date/time value using a formal format including the 24 hour time.  For example - "Saturday, January 1, 2003 - 14:01"
* @return String - A string representation of this date/time value.
*/
public String formalwithtime24Format() {
  return format("EEEE, MMMM d, yyyy - HH:mm");


/**
* Formats this date/time value using a long .  For example - "January 1, 2003"
* @return String - A string representation of this date/time value.
*/

public String longFormat() {
  return format("MMMM d, yyyy");


/**
* Formats this date/time value using a date/time format.  For example - "12:03AM January 1, 2003"
* @return String - A string representation of this date/time value.
*/

public String datetimeFormat() {
  return format("h:mma MMMM d, yyyy");
}

/**
* Formats this date/time value using a date/time format with 24 hour time.  For example - "17:03 January 1, 2003"
* @return String - A string representation of this date/time value.
*/

public String datetime24Format() {
  return format("HH:mm MMMM d, yyyy");
}



 
 
}
Avatar billede c_s_p Nybegynder
20. december 2005 - 12:18 #8
Tak for hjælpen
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester