Tabnine Logo
GregorianCalendar
Code IndexAdd Tabnine to your IDE (free)

How to use
GregorianCalendar
in
java.util

Best Java code snippets using java.util.GregorianCalendar (Showing top 20 results out of 17,649)

Refine searchRefine arrow

  • Calendar
  • Date
  • TimeZone
  • SimpleDateFormat
  • DateFormat
origin: stackoverflow.com

 final DateFormat format = new SimpleDateFormat("E. M/d");
final String dateStr = "Thu. 03/01";
final Date date = format.parse(dateStr);

GregorianCalendar gregory = new GregorianCalendar();
gregory.setTime(date);

XMLGregorianCalendar calendar = DatatypeFactory.newInstance()
    .newXMLGregorianCalendar(
      gregory);
origin: Tencent/tinker

/**
 * Gets the last modification time of this {@code ZipEntry}.
 *
 * @return the last modification time as the number of milliseconds since
 *         Jan. 1, 1970.
 */
public long getTime() {
  if (time != -1) {
    GregorianCalendar cal = new GregorianCalendar();
    cal.set(Calendar.MILLISECOND, 0);
    cal.set(1980 + ((modDate >> 9) & 0x7f), ((modDate >> 5) & 0xf) - 1,
        modDate & 0x1f, (time >> 11) & 0x1f, (time >> 5) & 0x3f,
        (time & 0x1f) << 1);
    return cal.getTime().getTime();
  }
  return -1;
}
origin: stagemonitor/stagemonitor

private GregorianCalendar getNowUTC() {
  final GregorianCalendar now = new GregorianCalendar();
  now.setTimeZone(TimeZone.getTimeZone("UTC"));
  now.setTime(new Date(clock.getTime()));
  return now;
}
origin: netty/netty

private Date computeDate() {
  cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
  cal.set(Calendar.MONTH, month);
  cal.set(Calendar.YEAR, year);
  cal.set(Calendar.HOUR_OF_DAY, hours);
  cal.set(Calendar.MINUTE, minutes);
  cal.set(Calendar.SECOND, seconds);
  return cal.getTime();
}
origin: Tencent/tinker

/**
 * Sets the modification time of this {@code ZipEntry}.
 *
 * @param value
 *            the modification time as the number of milliseconds since Jan.
 *            1, 1970.
 */
public void setTime(long value) {
  GregorianCalendar cal = new GregorianCalendar();
  cal.setTime(new Date(value));
  int year = cal.get(Calendar.YEAR);
  if (year < 1980) {
    modDate = 0x21;
    time = 0;
  } else {
    modDate = cal.get(Calendar.DATE);
    modDate = (cal.get(Calendar.MONTH) + 1 << 5) | modDate;
    modDate = ((cal.get(Calendar.YEAR) - 1980) << 9) | modDate;
    time = cal.get(Calendar.SECOND) >> 1;
    time = (cal.get(Calendar.MINUTE) << 5) | time;
    time = (cal.get(Calendar.HOUR_OF_DAY) << 11) | time;
  }
}
origin: blynkkk/blynk-server

/**
 * Sets the Date and Cache headers for the HTTP Response
 *
 * @param response
 *            HTTP response
 * @param fileToCache
 *            file to extract content type
 */
private static void setDateAndCacheHeaders(io.netty.handler.codec.http.HttpResponse response, File fileToCache) {
  SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);
  dateFormatter.setTimeZone(TimeZone.getTimeZone(HTTP_DATE_GMT_TIMEZONE));
  // Date header
  Calendar time = new GregorianCalendar();
  response.headers().set(DATE, dateFormatter.format(time.getTime()));
  // Add cache headers
  time.add(Calendar.SECOND, HTTP_CACHE_SECONDS);
  response.headers()
      .set(EXPIRES, dateFormatter.format(time.getTime()))
      .set(CACHE_CONTROL, "private, max-age=" + HTTP_CACHE_SECONDS)
      .set(LAST_MODIFIED, dateFormatter.format(new Date(fileToCache.lastModified())));
}
origin: stackoverflow.com

UTC = TimeZone.getTimeZone("UTC");
TimeZone.setDefault(UTC);
final Calendar c = new GregorianCalendar(UTC);
c.set(1, 0, 1, 0, 0, 0);
c.set(Calendar.MILLISECOND, 0);
BEGINNING_OF_TIME = c.getTime();
c.setTime(new Date(Long.MAX_VALUE));
END_OF_TIME = c.getTime();
final SimpleDateFormat sdf = new SimpleDateFormat(ISO_8601_24H_FULL_FORMAT);
sdf.setTimeZone(UTC);
System.out.println("sdf.format(BEGINNING_OF_TIME) = " + sdf.format(BEGINNING_OF_TIME));
System.out.println("sdf.format(END_OF_TIME) = " + sdf.format(END_OF_TIME));
System.out.println("sdf.format(new Date()) = " + sdf.format(new Date()));
System.out.println("sdf.parse(\"2015-04-28T14:23:38.521Z\") = " + sdf.parse("2015-04-28T14:23:38.521Z"));
System.out.println("sdf.parse(\"0001-01-01T00:00:00.000Z\") = " + sdf.parse("0001-01-01T00:00:00.000Z"));
origin: apache/maven

public MavenBuildTimestamp( Date time, String timestampFormat )
{
  if ( timestampFormat == null )
  {
    timestampFormat = DEFAULT_BUILD_TIMESTAMP_FORMAT;
  }
  if ( time == null )
  {
    time = new Date();
  }
  SimpleDateFormat dateFormat = new SimpleDateFormat( timestampFormat );
  dateFormat.setCalendar( new GregorianCalendar() );
  dateFormat.setTimeZone( DEFAULT_BUILD_TIME_ZONE );
  formattedTimestamp = dateFormat.format( time );
}
origin: stackoverflow.com

Date date1 = new Date(2011, Calendar.JULY, 3);
 Calendar date = new GregorianCalendar();
 date.setTime(date1);
 date.add(Calendar.DAY_OF_MONTH, -7);
 date2 = date.getTime();
origin: spring-projects/spring-framework

  @RequestMapping(method = RequestMethod.GET)
  public void handle(@CookieValue("date") Date date, Writer writer) throws IOException {
    assertEquals("Invalid path variable value", new GregorianCalendar(2008, 10, 18).getTime(), date);
    writer.write("test-" + new SimpleDateFormat("yyyy").format(date));
  }
}
origin: ankidroid/Anki-Android

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH-mm", Locale.US);
Calendar cal = new GregorianCalendar();
cal.setTimeInMillis(System.currentTimeMillis());
  try {
    len--;
    lastBackupDate = df.parse(deckBackups[len].getName().replaceAll(
        "^.*-(\\d{4}-\\d{2}-\\d{2}-\\d{2}-\\d{2}).apkg$", "$1"));
  } catch (ParseException e) {
if (lastBackupDate != null && lastBackupDate.getTime() + interval * 3600000L > Utils.intNow(1000) && !force) {
  Timber.d("performBackup: No backup created. Last backup younger than 5 hours");
  return false;
try {
  backupFilename = String.format(Utils.ENGLISH_LOCALE, colFile.getName().replace(".anki2", "")
      + "-%s.apkg", df.format(cal.getTime()));
} catch (UnknownFormatConversionException e) {
  Timber.e(e, "performBackup: error on creating backup filename");
origin: blynkkk/blynk-server

/**
 * Sets the Date header for the HTTP response
 *
 * @param response
 *            HTTP response
 */
private static void setDateHeader(FullHttpResponse response) {
  SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);
  dateFormatter.setTimeZone(TimeZone.getTimeZone(HTTP_DATE_GMT_TIMEZONE));
  Calendar time = new GregorianCalendar();
  response.headers().set(DATE, dateFormatter.format(time.getTime()));
}
origin: jaydenxiao2016/AndroidFire

public static String getNextHour(int i) {
  String curDateTime = null;
  try {
    SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(dateFormat);
    Calendar c = new GregorianCalendar();
    c.add(Calendar.HOUR_OF_DAY, i);
    curDateTime = mSimpleDateFormat.format(c.getTime());
  } catch (Exception e) {
    e.printStackTrace();
  }
  return curDateTime;
}
origin: square/moshi

 private Date newDate(int year, int month, int day, int hour, int offset) {
  Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
  calendar.set(year, month - 1, day, hour, 0, 0);
  calendar.set(Calendar.MILLISECOND, 0);
  return new Date(calendar.getTimeInMillis() - TimeUnit.HOURS.toMillis(offset));
 }
}
origin: apache/maven

DateFormat utcDateFormatter = new SimpleDateFormat( DEFAULT_SNAPSHOT_TIMESTAMP_FORMAT );
utcDateFormatter.setCalendar( new GregorianCalendar() );
utcDateFormatter.setTimeZone( DEFAULT_SNAPSHOT_TIME_ZONE );
snapshot.setTimestamp( utcDateFormatter.format( new Date() ) );
origin: org.mongodb/mongo-java-driver

} else if (b.containsField("$date")) {
  if (b.get("$date") instanceof Number) {
    o = new Date(((Number) b.get("$date")).longValue());
  } else {
    SimpleDateFormat format = new SimpleDateFormat(_msDateFormat);
    format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
    o = format.parse(b.get("$date").toString(), new ParsePosition(0));
      format = new SimpleDateFormat(_secDateFormat);
      format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
      o = format.parse(b.get("$date").toString(), new ParsePosition(0));
origin: spring-projects/spring-framework

@Test
public void testSetParameterValueWithDateAndCalendar() throws SQLException {
  java.util.Calendar cal = new GregorianCalendar();
  StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.DATE, null, cal);
  verify(preparedStatement).setDate(1, new java.sql.Date(cal.getTime().getTime()), cal);
}
origin: stackoverflow.com

 Long gmtTime =1317951113613L; // 2.32pm NZDT
Long timezoneAlteredTime = 0L;

if (offset != 0L) {
  int multiplier = (offset*60)*(60*1000);
  timezoneAlteredTime = gmtTime + multiplier;
} else {
  timezoneAlteredTime = gmtTime;
}

Calendar calendar = new GregorianCalendar();
calendar.setTimeInMillis(timezoneAlteredTime);

DateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");

formatter.setCalendar(calendar);
formatter.setTimeZone(TimeZone.getTimeZone(timeZone));

String newZealandTime = formatter.format(calendar.getTime());
origin: com.thoughtworks.xstream/xstream

public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
  reader.moveDown();
  long timeInMillis = Long.parseLong(reader.getValue());
  reader.moveUp();
  final String timeZone;
  if (reader.hasMoreChildren()) {
    reader.moveDown();
    timeZone = reader.getValue();
    reader.moveUp();
  } else { // backward compatibility to XStream 1.1.2 and below
    timeZone = TimeZone.getDefault().getID();
  }
  GregorianCalendar result = new GregorianCalendar();
  result.setTimeZone(TimeZone.getTimeZone(timeZone));
  result.setTime(new Date(timeInMillis)); // calendar.setTimeInMillis() not available under JDK 1.3
  return result;
}
origin: hibernate/hibernate-orm

@PostLoad
public void calculateAge() {
  Calendar birth = new GregorianCalendar();
  birth.setTime( dateOfBirth );
  Calendar now = new GregorianCalendar();
  now.setTime( new Date() );
  int adjust = 0;
  if ( now.get( Calendar.DAY_OF_YEAR ) - birth.get( Calendar.DAY_OF_YEAR ) < 0 ) {
    adjust = -1;
  }
  age = now.get( Calendar.YEAR ) - birth.get( Calendar.YEAR ) + adjust;
}
java.utilGregorianCalendar

Javadoc

GregorianCalendar is a concrete subclass of Calendarand provides the standard calendar used by most of the world.

The standard (Gregorian) calendar has 2 eras, BC and AD.

This implementation handles a single discontinuity, which corresponds by default to the date the Gregorian calendar was instituted (October 15, 1582 in some countries, later in others). The cutover date may be changed by the caller by calling setGregorianChange().

Historically, in those countries which adopted the Gregorian calendar first, October 4, 1582 was thus followed by October 15, 1582. This calendar models this correctly. Before the Gregorian cutover, GregorianCalendarimplements the Julian calendar. The only difference between the Gregorian and the Julian calendar is the leap year rule. The Julian calendar specifies leap years every four years, whereas the Gregorian calendar omits century years which are not divisible by 400.

GregorianCalendar implements proleptic Gregorian and Julian calendars. That is, dates are computed by extrapolating the current rules indefinitely far backward and forward in time. As a result, GregorianCalendar may be used for all years to generate meaningful and consistent results. However, dates obtained using GregorianCalendar are historically accurate only from March 1, 4 AD onward, when modern Julian calendar rules were adopted. Before this date, leap year rules were applied irregularly, and before 45 BC the Julian calendar did not even exist.

Prior to the institution of the Gregorian calendar, New Year's Day was March 25. To avoid confusion, this calendar always uses January 1. A manual adjustment may be made if desired for dates that are prior to the Gregorian changeover and which fall between January 1 and March 24.

Values calculated for the WEEK_OF_YEAR field range from 1 to 53. Week 1 for a year is the earliest seven day period starting on getFirstDayOfWeek() that contains at least getMinimalDaysInFirstWeek() days from that year. It thus depends on the values of getMinimalDaysInFirstWeek(), getFirstDayOfWeek(), and the day of the week of January 1. Weeks between week 1 of one year and week 1 of the following year are numbered sequentially from 2 to 52 or 53 (as needed).

For example, January 1, 1998 was a Thursday. If getFirstDayOfWeek() is MONDAY and getMinimalDaysInFirstWeek() is 4 (these are the values reflecting ISO 8601 and many national standards), then week 1 of 1998 starts on December 29, 1997, and ends on January 4, 1998. If, however, getFirstDayOfWeek() is SUNDAY, then week 1 of 1998 starts on January 4, 1998, and ends on January 10, 1998; the first three days of 1998 then are part of week 53 of 1997.

Values calculated for the WEEK_OF_MONTH field range from 0 or 1 to 4 or 5. Week 1 of a month (the days with WEEK_OF_MONTH = 1) is the earliest set of at least getMinimalDaysInFirstWeek()contiguous days in that month, ending on the day before getFirstDayOfWeek(). Unlike week 1 of a year, week 1 of a month may be shorter than 7 days, need not start on getFirstDayOfWeek(), and will not include days of the previous month. Days of a month before week 1 have a WEEK_OF_MONTH of 0.

For example, if getFirstDayOfWeek() is SUNDAYand getMinimalDaysInFirstWeek() is 4, then the first week of January 1998 is Sunday, January 4 through Saturday, January 10. These days have a WEEK_OF_MONTH of 1. Thursday, January 1 through Saturday, January 3 have a WEEK_OF_MONTH of 0. If getMinimalDaysInFirstWeek() is changed to 3, then January 1 through January 3 have a WEEK_OF_MONTH of 1.

Example:

 
// get the supported ids for GMT-08:00 (Pacific Standard Time) 
String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000); 
// if no ids were returned, something is wrong. get out. 
if (ids.length == 0) 
System.exit(0); 
// begin output 
System.out.println("Current Time"); 
// create a Pacific Standard Time time zone 
SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]); 
// set up rules for daylight savings time 
pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000); 
pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000); 
// create a GregorianCalendar with the Pacific Daylight time zone 
// and the current date and time 
Calendar calendar = new GregorianCalendar(pdt); 
Date trialTime = new Date(); 
calendar.setTime(trialTime); 
// print out a bunch of interesting things 
System.out.println("ERA: " + calendar.get(Calendar.ERA)); 
System.out.println("YEAR: " + calendar.get(Calendar.YEAR)); 
System.out.println("MONTH: " + calendar.get(Calendar.MONTH)); 
System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR)); 
System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH)); 
System.out.println("DATE: " + calendar.get(Calendar.DATE)); 
System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH)); 
System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR)); 
System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK)); 
System.out.println("DAY_OF_WEEK_IN_MONTH: " 
+ calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH)); 
System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM)); 
System.out.println("HOUR: " + calendar.get(Calendar.HOUR)); 
System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY)); 
System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE)); 
System.out.println("SECOND: " + calendar.get(Calendar.SECOND)); 
System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND)); 
System.out.println("ZONE_OFFSET: " 
+ (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000))); 
System.out.println("DST_OFFSET: " 
+ (calendar.get(Calendar.DST_OFFSET)/(60*60*1000))); 
System.out.println("Current Time, with hour reset to 3"); 
calendar.clear(Calendar.HOUR_OF_DAY); // so doesn't override 
calendar.set(Calendar.HOUR, 3); 
System.out.println("ERA: " + calendar.get(Calendar.ERA)); 
System.out.println("YEAR: " + calendar.get(Calendar.YEAR)); 
System.out.println("MONTH: " + calendar.get(Calendar.MONTH)); 
System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR)); 
System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH)); 
System.out.println("DATE: " + calendar.get(Calendar.DATE)); 
System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH)); 
System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR)); 
System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK)); 
System.out.println("DAY_OF_WEEK_IN_MONTH: " 
+ calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH)); 
System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM)); 
System.out.println("HOUR: " + calendar.get(Calendar.HOUR)); 
System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY)); 
System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE)); 
System.out.println("SECOND: " + calendar.get(Calendar.SECOND)); 
System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND)); 
System.out.println("ZONE_OFFSET: " 
+ (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000))); // in hours 
System.out.println("DST_OFFSET: " 
+ (calendar.get(Calendar.DST_OFFSET)/(60*60*1000))); // in hours 

Most used methods

  • <init>
  • getTime
  • setTime
  • get
  • set
  • getTimeInMillis
  • setTimeInMillis
  • add
  • getInstance
  • setTimeZone
  • clear
  • getTimeZone
  • clear,
  • getTimeZone,
  • isLeapYear,
  • setGregorianChange,
  • from,
  • getActualMaximum,
  • getGregorianChange,
  • before,
  • clone,
  • setLenient

Popular in Java

  • Making http post requests using okhttp
  • getContentResolver (Context)
  • getApplicationContext (Context)
  • orElseThrow (Optional)
    Return the contained value, if present, otherwise throw an exception to be created by the provided s
  • FileWriter (java.io)
    A specialized Writer that writes to a file in the file system. All write requests made by calling me
  • MalformedURLException (java.net)
    This exception is thrown when a program attempts to create an URL from an incorrect specification.
  • Proxy (java.net)
    This class represents proxy server settings. A created instance of Proxy stores a type and an addres
  • Executors (java.util.concurrent)
    Factory and utility methods for Executor, ExecutorService, ScheduledExecutorService, ThreadFactory,
  • Cipher (javax.crypto)
    This class provides access to implementations of cryptographic ciphers for encryption and decryption
  • Filter (javax.servlet)
    A filter is an object that performs filtering tasks on either the request to a resource (a servlet o
  • Top Vim plugins
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now