Example usage for java.util Calendar getTimeZone

List of usage examples for java.util Calendar getTimeZone

Introduction

In this page you can find the example usage for java.util Calendar getTimeZone.

Prototype

public TimeZone getTimeZone() 

Source Link

Document

Gets the time zone.

Usage

From source file:net.sourceforge.msscodefactory.cfasterisk.v2_1.CFAstSybase.CFAstSybaseSchema.java

public static String getTZTimeString(Calendar val) {
    if (val == null) {
        return ("null");
    } else {/* w w  w .  jav a  2 s .  c  o  m*/
        Calendar cal;
        if (val.getTimeZone().equals(getServerTimeZone())) {
            cal = val;
        } else {
            cal = new GregorianCalendar(getServerTimeZone());
            cal.setTimeInMillis(val.getTimeInMillis());
        }
        StringBuffer buff = new StringBuffer();
        Formatter fmt = new Formatter(buff);
        fmt.format("%1$04d", cal.get(Calendar.YEAR));
        buff.append("-");
        fmt.format("%1$02d", cal.get(Calendar.MONTH) + 1);
        buff.append("-");
        fmt.format("%1$02d", cal.get(Calendar.DAY_OF_MONTH));
        buff.append("T");
        fmt.format("%1$02d", cal.get(Calendar.HOUR_OF_DAY));
        buff.append(":");
        fmt.format("%1$02d", cal.get(Calendar.MINUTE));
        buff.append(":");
        fmt.format("%1$02d", cal.get(Calendar.SECOND));
        fmt.close();
        return (buff.toString());
    }
}

From source file:net.sourceforge.msscodefactory.cfasterisk.v2_1.CFAstSybase.CFAstSybaseSchema.java

public static String getTZTimestampString(Calendar val) {
    if (val == null) {
        return ("null");
    } else {//from  w  w  w. java2 s  .  co  m
        Calendar cal;
        if (val.getTimeZone().equals(getServerTimeZone())) {
            cal = val;
        } else {
            cal = new GregorianCalendar(getServerTimeZone());
            cal.setTimeInMillis(val.getTimeInMillis());
        }
        StringBuffer buff = new StringBuffer();
        Formatter fmt = new Formatter(buff);
        fmt.format("%1$04d", cal.get(Calendar.YEAR));
        buff.append("-");
        fmt.format("%1$02d", cal.get(Calendar.MONTH) + 1);
        buff.append("-");
        fmt.format("%1$02d", cal.get(Calendar.DAY_OF_MONTH));
        buff.append("T");
        fmt.format("%1$02d", cal.get(Calendar.HOUR_OF_DAY));
        buff.append(":");
        fmt.format("%1$02d", cal.get(Calendar.MINUTE));
        buff.append(":");
        fmt.format("%1$02d", cal.get(Calendar.SECOND));
        fmt.close();
        return (buff.toString());
    }
}

From source file:net.sourceforge.msscodefactory.cfasterisk.v2_1.CFAstSybase.CFAstSybaseSchema.java

public static String getQuotedTZDateString(Calendar val) {
    if (val == null) {
        return ("null");
    } else {//from w  w w  . j a v  a 2  s .  co m
        Calendar cal;
        if (val.getTimeZone().equals(getServerTimeZone())) {
            cal = val;
        } else {
            cal = new GregorianCalendar(getServerTimeZone());
            cal.setTimeInMillis(val.getTimeInMillis());
        }
        StringBuffer buff = new StringBuffer("'");
        Formatter fmt = new Formatter(buff);
        fmt.format("%1$04d", cal.get(Calendar.YEAR));
        buff.append("-");
        fmt.format("%1$02d", cal.get(Calendar.MONTH) + 1);
        buff.append("-");
        fmt.format("%1$02d", cal.get(Calendar.DAY_OF_MONTH));
        buff.append("T");
        fmt.format("%1$02d", cal.get(Calendar.HOUR_OF_DAY));
        buff.append(":");
        fmt.format("%1$02d", cal.get(Calendar.MINUTE));
        buff.append(":");
        fmt.format("%1$02d", cal.get(Calendar.SECOND));
        buff.append("'");
        fmt.close();
        return (buff.toString());
    }
}

From source file:net.sourceforge.msscodefactory.cfasterisk.v2_1.CFAstSybase.CFAstSybaseSchema.java

public static String getQuotedTZTimeString(Calendar val) {
    if (val == null) {
        return ("null");
    } else {/*from  w ww.ja  va2  s  . c o m*/
        Calendar cal;
        if (val.getTimeZone().equals(getServerTimeZone())) {
            cal = val;
        } else {
            cal = new GregorianCalendar(getServerTimeZone());
            cal.setTimeInMillis(val.getTimeInMillis());
        }
        StringBuffer buff = new StringBuffer("'");
        Formatter fmt = new Formatter(buff);
        fmt.format("%1$04d", cal.get(Calendar.YEAR));
        buff.append("-");
        fmt.format("%1$02d", cal.get(Calendar.MONTH) + 1);
        buff.append("-");
        fmt.format("%1$02d", cal.get(Calendar.DAY_OF_MONTH));
        buff.append("T");
        fmt.format("%1$02d", cal.get(Calendar.HOUR_OF_DAY));
        buff.append(":");
        fmt.format("%1$02d", cal.get(Calendar.MINUTE));
        buff.append(":");
        fmt.format("%1$02d", cal.get(Calendar.SECOND));
        buff.append("'");
        fmt.close();
        return (buff.toString());
    }
}

From source file:net.sourceforge.msscodefactory.cfasterisk.v2_1.CFAstSybase.CFAstSybaseSchema.java

public static String getQuotedTZTimestampString(Calendar val) {
    if (val == null) {
        return ("null");
    } else {//from w w  w  .  jav a 2s . c om
        Calendar cal;
        if (val.getTimeZone().equals(getServerTimeZone())) {
            cal = val;
        } else {
            cal = new GregorianCalendar(getServerTimeZone());
            cal.setTimeInMillis(val.getTimeInMillis());
        }
        StringBuffer buff = new StringBuffer("'");
        Formatter fmt = new Formatter(buff);
        fmt.format("%1$04d", cal.get(Calendar.YEAR));
        buff.append("-");
        fmt.format("%1$02d", cal.get(Calendar.MONTH) + 1);
        buff.append("-");
        fmt.format("%1$02d", cal.get(Calendar.DAY_OF_MONTH));
        buff.append("T");
        fmt.format("%1$02d", cal.get(Calendar.HOUR_OF_DAY));
        buff.append(":");
        fmt.format("%1$02d", cal.get(Calendar.MINUTE));
        buff.append(":");
        fmt.format("%1$02d", cal.get(Calendar.SECOND));
        buff.append("'");
        fmt.close();
        return (buff.toString());
    }
}

From source file:org.shredzone.cilla.service.search.strategy.AbstractSearchStrategy.java

@SuppressWarnings("unchecked")
@Override//from   ww  w.j  av a 2 s .c om
public Set<Integer> fetchPageDays(SearchResultImpl result, Calendar calendar) throws CillaServiceException {
    if (calendar == null) {
        return Collections.emptySet();
    }

    Calendar start = DateUtils.beginningOfMonth(calendar);
    Calendar end = DateUtils.beginningOfNextMonth(calendar);

    Criteria crit = createCriteria(result);
    crit.add(Restrictions.ge(pageOrder.getColumn(), start.getTime()));
    crit.add(Restrictions.lt(pageOrder.getColumn(), end.getTime()));

    // Sadly there is no HQL function to get the calendary day of a timestamp,
    // so we have to do all the hard work ourselves, to avoid SQL dialects. :(
    crit.setProjection(Projections.property(pageOrder.getColumn()));

    Calendar cal = Calendar.getInstance(calendar.getTimeZone());

    Set<Integer> daysResult = new HashSet<>();
    for (Date day : (List<Date>) crit.list()) {
        cal.setTime(day);
        daysResult.add(cal.get(Calendar.DAY_OF_MONTH));
    }

    return daysResult;
}

From source file:no.digipost.android.gui.metadata.AppointmentView.java

private void addToCalendar(Metadata appointment) {
    String description = appointment.subTitle + "\n\n" + appointment.getInfoTitleAndText();

    Calendar beginTime = Calendar.getInstance();
    beginTime.setTime(appointment.getStartDate());

    Calendar endTime = Calendar.getInstance();
    endTime.setTime(appointment.getEndDate());

    ContentResolver cr = getActivity().getContentResolver();
    ContentValues values = new ContentValues();
    values.put(CalendarContract.Events.DTSTART, beginTime.getTimeInMillis());
    values.put(CalendarContract.Events.DTEND, endTime.getTimeInMillis());
    values.put(CalendarContract.Events.TITLE, appointment.title);
    values.put(CalendarContract.Events.EVENT_LOCATION,
            appointment.getPlaceAddress() + " - " + appointment.getPlace());
    values.put(CalendarContract.Events.DESCRIPTION, description);
    values.put(CalendarContract.Events.EVENT_TIMEZONE, beginTime.getTimeZone().toString());
    values.put(CalendarContract.Events.CALENDAR_ID, 1);
    Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);

}

From source file:org.pentaho.di.core.row.ValueDataUtil.java

/**
 * Returns the number of days that have elapsed between dataA and dataB.
 *
 * @param metaA//from  w  ww.j  a  v  a 2s .  c  o m
 * @param dataA
 *          The "end date"
 * @param metaB
 * @param dataB
 *          The "start date"
 * @param resultType
 *          The "result type" (ms, s, mn, h, d)
 * @return Number of days
 * @throws KettleValueException
 */

public static Object DateDiff(ValueMetaInterface metaA, Object dataA, ValueMetaInterface metaB, Object dataB,
        String resultType) throws KettleValueException {

    if (dataA != null && dataB != null) {
        Date startDate = metaB.getDate(dataB);
        Date endDate = metaA.getDate(dataA);

        Calendar stDateCal = Calendar.getInstance();
        Calendar endDateCal = Calendar.getInstance();
        stDateCal.setTime(startDate);
        endDateCal.setTime(endDate);

        long endL = endDateCal.getTimeInMillis()
                + endDateCal.getTimeZone().getOffset(endDateCal.getTimeInMillis());
        long startL = stDateCal.getTimeInMillis()
                + stDateCal.getTimeZone().getOffset(stDateCal.getTimeInMillis());
        long diff = endL - startL;

        if (Const.isEmpty(resultType)) {
            return new Long(diff / 86400000);
        } else if (resultType.equals("ms")) {
            return new Long(diff);
        } else if (resultType.equals("s")) {
            return new Long(diff / 1000); // second
        } else if (resultType.equals("mn")) {
            return new Long(diff / 60000); // minute
        } else if (resultType.equals("h")) {
            return new Long(diff / 3600000); // hour
        } else if (resultType.equals("d")) {
            return new Long(diff / 86400000);
        } else {
            throw new KettleValueException("Unknown result type option '" + resultType + "'");
        }
    } else {
        return null;
    }
}

From source file:com.google.ytd.SubmitActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.submit);

    this.authorizer = new GlsAuthorizer.GlsAuthorizerFactory().getAuthorizer(this,
            GlsAuthorizer.YOUTUBE_AUTH_TOKEN_TYPE);

    dbHelper = new DbHelper(this);
    dbHelper = dbHelper.open();// ww w  . java  2s .  co m

    Intent intent = this.getIntent();
    this.videoUri = intent.getData();
    this.ytdDomain = intent.getExtras().getString(DbHelper.YTD_DOMAIN);
    this.assignmentId = intent.getExtras().getString(DbHelper.ASSIGNMENT_ID);
    this.title = intent.getExtras().getString(DbHelper.DESCRIPTION);
    this.instructions = intent.getExtras().getString(DbHelper.INSTRUCTIONS);

    this.domainHeader = (TextView) this.findViewById(R.id.domainHeader);
    domainHeader.setText(SettingActivity.getYtdDomains(this).get(this.ytdDomain));

    this.preferences = this.getSharedPreferences(MainActivity.SHARED_PREF_NAME, Activity.MODE_PRIVATE);
    this.youTubeName = preferences.getString(DbHelper.YT_ACCOUNT, null);

    final Button submitButton = (Button) findViewById(R.id.submitButton);
    submitButton.setEnabled(false);

    submitButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            showDialog(DIALOG_LEGAL);
        }
    });

    Button cancelButton = (Button) findViewById(R.id.cancelButton);
    cancelButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            setResult(RESULT_CANCELED);
            finish();
        }
    });

    EditText titleEdit = (EditText) findViewById(R.id.submitTitle);
    titleEdit.setText(title);
    titleEdit.setEnabled(false);
    enableSubmitIfReady();

    EditText descriptionEdit = (EditText) findViewById(R.id.submitDescription);
    descriptionEdit.setText(instructions);
    descriptionEdit.setEnabled(false);

    Cursor cursor = this.managedQuery(this.videoUri, null, null, null, null);

    if (cursor.getCount() == 0) {
        Log.d(LOG_TAG, "not a valid video uri");
        Toast.makeText(SubmitActivity.this, "not a valid video uri", Toast.LENGTH_LONG).show();
    } else {
        getVideoLocation();

        if (cursor.moveToFirst()) {

            long id = cursor.getLong(cursor.getColumnIndex(Video.VideoColumns._ID));
            this.dateTaken = new Date(cursor.getLong(cursor.getColumnIndex(Video.VideoColumns.DATE_TAKEN)));

            SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, MMM d, yyyy hh:mm aaa");
            Configuration userConfig = new Configuration();
            Settings.System.getConfiguration(getContentResolver(), userConfig);
            Calendar cal = Calendar.getInstance(userConfig.locale);
            TimeZone tz = cal.getTimeZone();

            dateFormat.setTimeZone(tz);

            TextView dateTakenView = (TextView) findViewById(R.id.dateCaptured);
            dateTakenView.setText("Date captured: " + dateFormat.format(dateTaken));

            ImageView thumbnail = (ImageView) findViewById(R.id.thumbnail);
            ContentResolver crThumb = getContentResolver();
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 1;
            Bitmap curThumb = MediaStore.Video.Thumbnails.getThumbnail(crThumb, id,
                    MediaStore.Video.Thumbnails.MICRO_KIND, options);
            thumbnail.setImageBitmap(curThumb);
        }
    }
}

From source file:helper.lang.DateHelperTest.java

@Test
public void testGetRangeDaysBeforeTodayUtc() {
    int days = 1;
    Calendar nowUtc = DateHelper.nowUtc();

    CalendarRange range = DateHelper.getRangeFullDaysBefore(nowUtc, days);
    Calendar start = range.getStart();
    Calendar end = range.getEnd();
    System.out.println(DateFormatUtils.SMTP_DATETIME_FORMAT.format(start));
    System.out.println(DateFormatUtils.SMTP_DATETIME_FORMAT.format(end));

    // end date is midnight of date passed in
    assertEquals(nowUtc.get(Calendar.YEAR), end.get(Calendar.YEAR));
    assertEquals(nowUtc.get(Calendar.MONTH), end.get(Calendar.MONTH));
    assertEquals(nowUtc.get(Calendar.DATE), end.get(Calendar.DATE));
    assertEquals(0, end.get(Calendar.HOUR));
    assertEquals(0, end.get(Calendar.MINUTE));
    assertEquals(0, end.get(Calendar.SECOND));
    assertEquals(0, end.get(Calendar.MILLISECOND));
    assertEquals(0, end.get(Calendar.HOUR_OF_DAY));
    assertEquals(DateHelper.UTC_TIME_ZONE, end.getTimeZone());

    // start date is 11:59:59 or 'days' time
    nowUtc.add(Calendar.DATE, -(days + 2));
    assertEquals(nowUtc.get(Calendar.YEAR), start.get(Calendar.YEAR));
    assertEquals(nowUtc.get(Calendar.MONTH), start.get(Calendar.MONTH));
    assertEquals(nowUtc.get(Calendar.DATE), start.get(Calendar.DATE));
    assertEquals(11, start.get(Calendar.HOUR));
    assertEquals(23, start.get(Calendar.HOUR_OF_DAY));
    assertEquals(59, start.get(Calendar.MINUTE));
    assertEquals(59, start.get(Calendar.SECOND));
    assertEquals(999, start.get(Calendar.MILLISECOND));
    assertEquals(DateHelper.UTC_TIME_ZONE, start.getTimeZone());

}