Java Code Examples for java.util.Date#getDate()

The following examples show how to use java.util.Date#getDate() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
@SuppressWarnings("deprecation")
public void setDate(Date date) {
    int dayOfMonth = date.getDate();
    if (monthNameVisible) {
        caption.setText(dayOfMonth + " " + calendar.getMonthNames()[date.getMonth()]);
    } else {
        caption.setText("" + dayOfMonth);
    }

    if (dayOfMonth == 1) {
        addStyleName("firstDay");
    } else {
        removeStyleName("firstDay");
    }

    this.date = date;
}
 
Example 2
@SuppressWarnings("deprecation")
public static void main(String args[])
{
    GregorianCalendar cal = new GregorianCalendar();
    cal.set(Calendar.YEAR, 1997);
    cal.set(Calendar.DAY_OF_YEAR, 1);
    Date d = cal.getTime();             // Should be Jan 1
    if (d.getMonth() != 0 || d.getDate() != 1) {
        throw new RuntimeException("Date isn't Jan 1");
    }
}
 
Example 3
public static long javaToDosTime(long time) {
    Date d = new Date(time);
    int year = d.getYear() + 1900;
    if (year < 1980) {
        return (1 << 21) | (1 << 16);
    }
    return (year - 1980) << 25 | (d.getMonth() + 1) << 21 |
           d.getDate() << 16 | d.getHours() << 11 | d.getMinutes() << 5 |
           d.getSeconds() >> 1;
}
 
Example 4
public static long javaToDosTime(long time) {
    Date d = new Date(time);
    int year = d.getYear() + 1900;
    if (year < 1980) {
        return (1 << 21) | (1 << 16);
    }
    return (year - 1980) << 25 | (d.getMonth() + 1) << 21 |
           d.getDate() << 16 | d.getHours() << 11 | d.getMinutes() << 5 |
           d.getSeconds() >> 1;
}
 
Example 5
/**
 * Converts Java time to DOS time.
 */
@SuppressWarnings("deprecation") // Use of date methods
public static long javaToDosTime(long time) {
    Date d = new Date(time);
    int year = d.getYear() + 1900;
    if (year < 1980) {
        return (1 << 21) | (1 << 16);
    }
    return (year - 1980) << 25 | (d.getMonth() + 1) << 21 |
           d.getDate() << 16 | d.getHours() << 11 | d.getMinutes() << 5 |
           d.getSeconds() >> 1;
}
 
Example 6
/**
* Get the order ID
* @return
*/
private String getOrderID() {
  Date d = new Date();
  return String.valueOf((d.getYear() + 1900)) + (d.getMonth() < 10 ? "0" + 
  		(d.getMonth() + 1) : (d.getMonth() + 1)) + 
  		(d.getDate() < 10 ? "0" + d.getDate() : d.getDate()) + 
  		"-" + MER_ID + "-" + System.currentTimeMillis();
}
 
Example 7
public static long javaToDosTime(long time) {
    Date d = new Date(time);
    int year = d.getYear() + 1900;
    if (year < 1980) {
        return (1 << 21) | (1 << 16);
    }
    return (year - 1980) << 25 | (d.getMonth() + 1) << 21 |
           d.getDate() << 16 | d.getHours() << 11 | d.getMinutes() << 5 |
           d.getSeconds() >> 1;
}
 
Example 8
Source Project: astor   File: LocalDate.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the date time as a <code>java.util.Date</code>.
 * <p>
 * The <code>Date</code> object created has exactly the same year, month and day
 * as this date. The time will be set to the earliest valid time for that date.
 * <p>
 * Converting to a JDK Date is full of complications as the JDK Date constructor
 * doesn't behave as you might expect around DST transitions. This method works
 * by taking a first guess and then adjusting the JDK date until it has the
 * earliest valid instant. This also handles the situation where the JDK time
 * zone data differs from the Joda-Time time zone data.
 *
 * @return a Date initialised with this date, never null
 * @since 2.0
 */
@SuppressWarnings("deprecation")
public Date toDate() {
    int dom = getDayOfMonth();
    Date date = new Date(getYear() - 1900, getMonthOfYear() - 1, dom);
    LocalDate check = LocalDate.fromDateFields(date);
    if (check.isBefore(this)) {
        // DST gap (no midnight)
        // move forward in units of one hour until date correct
        while (check.equals(this) == false) {
            date.setTime(date.getTime() + 3600000);
            check = LocalDate.fromDateFields(date);
        }
        // move back in units of one second until date wrong
        while (date.getDate() == dom) {
            date.setTime(date.getTime() - 1000);
        }
        // fix result
        date.setTime(date.getTime() + 1000);
    } else if (check.equals(this)) {
        // check for DST overlap (two midnights)
        Date earlier = new Date(date.getTime() - TimeZone.getDefault().getDSTSavings());
        if (earlier.getDate() == dom) {
            date = earlier;
        }
    }
    return date;
}
 
Example 9
Source Project: astor   File: LocalDate.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the date time as a <code>java.util.Date</code>.
 * <p>
 * The <code>Date</code> object created has exactly the same year, month and day
 * as this date. The time will be set to the earliest valid time for that date.
 * <p>
 * Converting to a JDK Date is full of complications as the JDK Date constructor
 * doesn't behave as you might expect around DST transitions. This method works
 * by taking a first guess and then adjusting the JDK date until it has the
 * earliest valid instant. This also handles the situation where the JDK time
 * zone data differs from the Joda-Time time zone data.
 *
 * @return a Date initialised with this date, never null
 * @since 2.0
 */
@SuppressWarnings("deprecation")
public Date toDate() {
    int dom = getDayOfMonth();
    Date date = new Date(getYear() - 1900, getMonthOfYear() - 1, dom);
    LocalDate check = LocalDate.fromDateFields(date);
    if (check.isBefore(this)) {
        // DST gap (no midnight)
        // move forward in units of one hour until date correct
        while (check.equals(this) == false) {
            date.setTime(date.getTime() + 3600000);
            check = LocalDate.fromDateFields(date);
        }
        // move back in units of one second until date wrong
        while (date.getDate() == dom) {
            date.setTime(date.getTime() - 1000);
        }
        // fix result
        date.setTime(date.getTime() + 1000);
    } else if (check.equals(this)) {
        // check for DST overlap (two midnights)
        Date earlier = new Date(date.getTime() - TimeZone.getDefault().getDSTSavings());
        if (earlier.getDate() == dom) {
            date = earlier;
        }
    }
    return date;
}
 
Example 10
Source Project: jdk8u60   File: ZipUtils.java    License: GNU General Public License v2.0 5 votes vote down vote up
public static long javaToDosTime(long time) {
    Date d = new Date(time);
    int year = d.getYear() + 1900;
    if (year < 1980) {
        return (1 << 21) | (1 << 16);
    }
    return (year - 1980) << 25 | (d.getMonth() + 1) << 21 |
           d.getDate() << 16 | d.getHours() << 11 | d.getMinutes() << 5 |
           d.getSeconds() >> 1;
}
 
Example 11
public static String[] get6days(boolean returnString)
{
    Date d=getToday();


    String [] days=new String[6];
    for (int i=0;i<6;i++)
    {
        Date t=add(d,i-5);
        days[i]=t.getMonth()+1+"."+t.getDate();
    }
    return days;
}
 
Example 12
Source Project: hottub   File: ZipUtils.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Converts Java time to DOS time.
 */
@SuppressWarnings("deprecation") // Use of date methods
private static long javaToDosTime(long time) {
    Date d = new Date(time);
    int year = d.getYear() + 1900;
    if (year < 1980) {
        return ZipEntry.DOSTIME_BEFORE_1980;
    }
    return (year - 1980) << 25 | (d.getMonth() + 1) << 21 |
           d.getDate() << 16 | d.getHours() << 11 | d.getMinutes() << 5 |
           d.getSeconds() >> 1;
}
 
Example 13
Source Project: iaf   File: FileUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static File getRollingFile(String directory, String filenamePrefix, String dateformat, String filenameSuffix, int retentionDays) {
	
	final long millisPerDay=24*60*60*1000;

	if (directory==null) {
		return null;
	}
	Date now=new Date();

	String filename=filenamePrefix+DateUtils.format(now,dateformat)+filenameSuffix;
	File result = new File(directory+"/"+filename);
	if (!result.exists()) {
		int year=now.getYear();
		int month=now.getMonth();
		int date=now.getDate();
	
		long thisMorning = new Date(year, month, date).getTime();

		long deleteBefore = thisMorning - retentionDays * millisPerDay;

		WildCardFilter filter = new WildCardFilter(filenamePrefix+"*"+filenameSuffix);
		File dir = new File(directory);
		File[] files = dir.listFiles(filter);

		int count = (files == null ? 0 : files.length);
		for (int i = 0; i < count; i++) {
			File file = files[i];
			if (file.isDirectory()) {
				continue;
			}
			if (file.lastModified()<deleteBefore) {
				file.delete();
			}
		}
	}
	
	return result;

}
 
Example 14
public static void main(String[] args) {
    TimeZone defaultTimeZone = TimeZone.getDefault();
    int errors = 0;

    Calendar cal = new GregorianCalendar(BEGIN_YEAR, MARCH, 1);
    String[] tzids = TimeZone.getAvailableIDs();
    try {
        for (String id : tzids) {
            TimeZone tz = TimeZone.getTimeZone(id);
            if (!tz.useDaylightTime()) {
                continue;
            }
            TimeZone.setDefault(tz);

          dateloop:
            // Use future dates because sun.util.calendar.ZoneInfo
            // delegates offset transition calculations to a SimpleTimeZone
            // (after 2038 as of JDK7).
            for (int year = BEGIN_YEAR; year < END_YEAR; year++) {
                for (int month = MARCH; month <= NOVEMBER; month++) {
                    cal.set(year, month, 1, 15, 0, 0);
                    int maxDom = cal.getActualMaximum(DAY_OF_MONTH);
                    for (int dom = 1; dom <= maxDom; dom++) {
                        Date date = new Date(year - 1900, month, dom);
                        if (date.getYear()+1900 != year
                            || date.getMonth() != month
                            || date.getDate() != dom) {
                            System.err.printf("%s: got %04d-%02d-%02d, expected %04d-%02d-%02d%n",
                                              id,
                                              date.getYear() + 1900,
                                              date.getMonth() + 1,
                                              date.getDate(),
                                              year,
                                              month + 1,
                                              dom);
                            errors++;
                            break dateloop;
                        }
                    }
                }
            }
        }
    } finally {
        // Restore the default TimeZone.
        TimeZone.setDefault(defaultTimeZone);
    }
    if (errors > 0) {
        throw new RuntimeException("Transition test failed");
    }
}
 
Example 15
public static void main(String[] args) {
    TimeZone defaultTimeZone = TimeZone.getDefault();
    int errors = 0;

    Calendar cal = new GregorianCalendar(BEGIN_YEAR, MARCH, 1);
    String[] tzids = TimeZone.getAvailableIDs();
    try {
        for (String id : tzids) {
            TimeZone tz = TimeZone.getTimeZone(id);
            if (!tz.useDaylightTime()) {
                continue;
            }
            TimeZone.setDefault(tz);

          dateloop:
            // Use future dates because sun.util.calendar.ZoneInfo
            // delegates offset transition calculations to a SimpleTimeZone
            // (after 2038 as of JDK7).
            for (int year = BEGIN_YEAR; year < END_YEAR; year++) {
                for (int month = MARCH; month <= NOVEMBER; month++) {
                    cal.set(year, month, 1, 15, 0, 0);
                    int maxDom = cal.getActualMaximum(DAY_OF_MONTH);
                    for (int dom = 1; dom <= maxDom; dom++) {
                        Date date = new Date(year - 1900, month, dom);
                        if (date.getYear()+1900 != year
                            || date.getMonth() != month
                            || date.getDate() != dom) {
                            System.err.printf("%s: got %04d-%02d-%02d, expected %04d-%02d-%02d%n",
                                              id,
                                              date.getYear() + 1900,
                                              date.getMonth() + 1,
                                              date.getDate(),
                                              year,
                                              month + 1,
                                              dom);
                            errors++;
                            break dateloop;
                        }
                    }
                }
            }
        }
    } finally {
        // Restore the default TimeZone.
        TimeZone.setDefault(defaultTimeZone);
    }
    if (errors > 0) {
        throw new RuntimeException("Transition test failed");
    }
}
 
Example 16
/**
* @return true if a date occurs on the same day as today.
*/
private boolean onSameDay(Date date, Date now) {
  return (date.getDate() == now.getDate())
      && (date.getMonth() == now.getMonth())
      && (date.getYear() == now.getYear());
}
 
Example 17
Source Project: AutoTest   File: TimeUtils.java    License: MIT License 4 votes vote down vote up
private static boolean isYestYesterday(Date date) {
    Date now = new Date();
    return (date.getYear() == now.getYear()) && (date.getMonth() == now.getMonth()) && (date.getDate() + 1 == now.getDate());
}
 
Example 18
public static void main(String[] args) {
    TimeZone defaultTimeZone = TimeZone.getDefault();
    int errors = 0;

    Calendar cal = new GregorianCalendar(BEGIN_YEAR, MARCH, 1);
    String[] tzids = TimeZone.getAvailableIDs();
    try {
        for (String id : tzids) {
            TimeZone tz = TimeZone.getTimeZone(id);
            if (!tz.useDaylightTime()) {
                continue;
            }
            TimeZone.setDefault(tz);

          dateloop:
            // Use future dates because sun.util.calendar.ZoneInfo
            // delegates offset transition calculations to a SimpleTimeZone
            // (after 2038 as of JDK7).
            for (int year = BEGIN_YEAR; year < END_YEAR; year++) {
                for (int month = MARCH; month <= NOVEMBER; month++) {
                    cal.set(year, month, 1, 15, 0, 0);
                    int maxDom = cal.getActualMaximum(DAY_OF_MONTH);
                    for (int dom = 1; dom <= maxDom; dom++) {
                        Date date = new Date(year - 1900, month, dom);
                        if (date.getYear()+1900 != year
                            || date.getMonth() != month
                            || date.getDate() != dom) {
                            System.err.printf("%s: got %04d-%02d-%02d, expected %04d-%02d-%02d%n",
                                              id,
                                              date.getYear() + 1900,
                                              date.getMonth() + 1,
                                              date.getDate(),
                                              year,
                                              month + 1,
                                              dom);
                            errors++;
                            break dateloop;
                        }
                    }
                }
            }
        }
    } finally {
        // Restore the default TimeZone.
        TimeZone.setDefault(defaultTimeZone);
    }
    if (errors > 0) {
        throw new RuntimeException("Transition test failed");
    }
}
 
Example 19
Source Project: astor   File: LocalDate.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * Constructs a LocalDate from a <code>java.util.Date</code>
 * using exactly the same field values.
 * <p>
 * Each field is queried from the Date and assigned to the LocalDate.
 * This is useful if you have been using the Date as a local date,
 * ignoring the zone.
 * <p>
 * One advantage of this method is that this method is unaffected if the
 * version of the time zone data differs between the JDK and Joda-Time.
 * That is because the local field values are transferred, calculated using
 * the JDK time zone data and without using the Joda-Time time zone data.
 * <p>
 * This factory method always creates a LocalDate with ISO chronology.
 *
 * @param date  the Date to extract fields from, not null
 * @return the created local date, not null
 * @throws IllegalArgumentException if the calendar is null
 * @throws IllegalArgumentException if the date is invalid for the ISO chronology
 */
@SuppressWarnings("deprecation")
public static LocalDate fromDateFields(Date date) {
    if (date == null) {
        throw new IllegalArgumentException("The date must not be null");
    }
    if (date.getTime() < 0) {
        // handle years in era BC
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(date);
        return fromCalendarFields(cal);
    }
    return new LocalDate(
        date.getYear() + 1900,
        date.getMonth() + 1,
        date.getDate()
    );
}
 
Example 20
Source Project: coming   File: Arja_0049_s.java    License: MIT License 3 votes vote down vote up
/**
 * Constructs a MonthDay from a <code>java.util.Date</code>
 * using exactly the same field values avoiding any time zone effects.
 * <p>
 * Each field is queried from the Date and assigned to the MonthDay.
 * <p>
 * This factory method always creates a MonthDay with ISO chronology.
 *
 * @param date  the Date to extract fields from
 * @return the created MonthDay, never null
 * @throws IllegalArgumentException if the calendar is null
 * @throws IllegalArgumentException if the monthOfYear or dayOfMonth is invalid for the ISO chronology
 */
@SuppressWarnings("deprecation")
public static MonthDay fromDateFields(Date date) {
    if (date == null) {
        throw new IllegalArgumentException("The date must not be null");
    }
    return new MonthDay(date.getMonth() + 1, date.getDate());
}