Example usage for java.util Calendar setTimeInMillis

List of usage examples for java.util Calendar setTimeInMillis

Introduction

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

Prototype

public void setTimeInMillis(long millis) 

Source Link

Document

Sets this Calendar's current time from the given long value.

Usage

From source file:com.vmware.identity.rest.core.server.util.VerificationUtil.java

/**
 * Verify the issued-at and expires-at dates in an access token
 *
 * @param token the token to verify/*from  w  w  w.  j a v  a  2 s .c om*/
 * @param skew the amount of skew to allow in milliseconds
 * @param sm a string manager to get the exception messages from
 *
 * @throws InvalidTokenException if the token is at an invalid date
 */
public static void verifyTimestamps(AccessToken token, long skew, StringManager sm)
        throws InvalidTokenException {
    Calendar now = Calendar.getInstance();
    Calendar issuedAt = Calendar.getInstance();
    Calendar expiresAt = Calendar.getInstance();

    if (token.getIssueTime() != null) {
        issuedAt.setTimeInMillis(token.getIssueTime().getTime() - skew);
    }

    if (token.getExpirationTime() != null) {
        expiresAt.setTimeInMillis(token.getExpirationTime().getTime() + skew);
    }

    if (token.getIssueTime() == null || issuedAt.after(now)) {
        throw new InvalidTokenException(sm.getString("auth.ite.bad.issue", issuedAt.getTime(), now.getTime()));
    }

    if (token.getExpirationTime() == null || expiresAt.before(now)) {
        throw new InvalidTokenException(
                sm.getString("auth.ite.bad.expiry", expiresAt.getTime(), now.getTime()));
    }
}

From source file:com.hemou.android.util.StrUtils.java

/**
 * {@link https://en.wikipedia.org/wiki/List_of_time_zones_by_country}
 * @param time/* ww w . jav a  2  s  .com*/
 * @return
 */
public static String convertTimeWithTimeZome(long time) {

    Calendar cal = Calendar.getInstance();
    cal.setTimeZone(TimeZone.getTimeZone("UTC"));
    cal.setTimeInMillis(time);
    return (cal.get(Calendar.YEAR) + " " + (cal.get(Calendar.MONTH) + 1) + " " + cal.get(Calendar.DAY_OF_MONTH)
            + " " + cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE));

}

From source file:com.hurence.logisland.utils.DateUtils.java

public static String outputDateInfo(Date d) {
    String output = "";
    Calendar c = GregorianCalendar.getInstance(tz);
    c.setTimeInMillis(d.getTime());
    TimeZone tzCal = c.getTimeZone();

    output += "Date:                         " + d + "\n"; // toString uses current system TimeZone
    output += "Date Millis:                  " + d.getTime() + "\n";
    output += "Cal Millis:                   " + c.getTimeInMillis() + "\n";
    output += "Cal To Date Millis:           " + c.getTime().getTime() + "\n";
    output += "Cal TimeZone Name:            " + tzCal.getDisplayName() + "\n";
    output += "Cal TimeZone ID:              " + tzCal.getID() + "\n";
    output += "Cal TimeZone DST Name:        " + tzCal.getDisplayName(true, TimeZone.SHORT) + "\n";
    output += "Cal TimeZone Standard Name:   " + tzCal.getDisplayName(false, TimeZone.SHORT) + "\n";
    output += "In DayLight:                  " + tzCal.inDaylightTime(d) + "\n";

    output += "" + "\n";
    output += "Day Of Month:                 " + c.get(Calendar.DAY_OF_MONTH) + "\n";
    output += "Month Of Year:                " + c.get(Calendar.MONTH) + "\n";
    output += "Year:                         " + c.get(Calendar.YEAR) + "\n";

    output += "Hour Of Day:                  " + c.get(Calendar.HOUR_OF_DAY) + "\n";
    output += "Minute:                       " + c.get(Calendar.MINUTE) + "\n";
    output += "Second:                       " + c.get(Calendar.SECOND) + "\n";

    return output;
}

From source file:com.etime.ETimeUtils.java

/**
 * Get the calculated eight hour punch. The eight hour punch is identical to the punch that is need for the user to
 * log exactly 8 hours for today./*from  w  ww .j a v a2s .c  o  m*/
 *
 * @param punches A list of punches logged today by a given user.
 * @return The calculated eight hour punch.
 */
protected static Punch getEightHrPunch(List<Punch> punches) {
    if (punches == null || punches.isEmpty())
        return null;

    Punch eightHrPunch = new Punch();
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(clockOutAt(punches));
    eightHrPunch.setCalendar(calendar);

    return eightHrPunch;
}

From source file:de.unikonstanz.winter.crossref.node.doi.CrossrefDoiNodeModel.java

private static DataCell nodeToDateCell(final JsonNode node) {
    if (CrossrefUtil.isNull(node)) {
        return new MissingCell(null);
    }/*from  ww  w  . j a va 2 s .c  o  m*/
    JsonNode timestamp = node.get("timestamp");
    if (CrossrefUtil.isNull(timestamp)) {
        return new MissingCell(null);
    }
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(timestamp.asLong());
    return new DateAndTimeCell(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
            calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR_OF_DAY),
            calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND), calendar.get(Calendar.MILLISECOND));
}

From source file:bammerbom.ultimatecore.bukkit.resources.utils.UuidUtil.java

public static void loadPlayers() {
    File directory = new File(r.getUC().getDataFolder() + File.separator + "Players");
    if (!directory.exists()) {
        directory.mkdirs();//w w w .  j ava 2  s. c o m
    }
    ArrayList<UUID> request = null;
    for (OfflinePlayer p : r.getOfflinePlayers()) {
        if (p.getUniqueId() == null) {
            continue;
        }
        final File file = new File(r.getUC().getDataFolder() + File.separator + "Players" + File.separator
                + p.getUniqueId() + ".yml");
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                ErrorLogger.log(e, "Failed to create new file for " + p.getUniqueId());
            }
        }
        Config conf = new Config(file);
        if (p.getName() == null) {
            if (!conf.contains("name")) {
                if (request == null) {
                    request = new ArrayList<>();
                }
                request.add(p.getUniqueId());
            }
        } else {
            if (!conf.contains("name")) {
                conf.set("name", p.getName());
                conf.save();
                if (!conf.contains("names")) {
                    ArrayList<String> names = new ArrayList<>();
                    Calendar timeCal = Calendar.getInstance();
                    timeCal.setTimeInMillis(System.currentTimeMillis());
                    String date = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(timeCal.getTime());
                    names.add(p.getName() + " - " + date);
                    conf.set("names", names);
                    conf.save();
                }
            } else {
                if (!conf.contains("names")) {
                    ArrayList<String> names = new ArrayList<>();
                    Calendar timeCal = Calendar.getInstance();
                    timeCal.setTimeInMillis(System.currentTimeMillis());
                    String date = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(timeCal.getTime());
                    names.add(p.getName() + " - " + date);
                    conf.set("names", names);
                    conf.save();
                }
                if (!conf.getString("name").equals(p.getName())) {
                    String oldname = conf.getString("name");
                    Calendar timeCal = Calendar.getInstance();
                    timeCal.setTimeInMillis(System.currentTimeMillis());
                    String date = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(timeCal.getTime());
                    List<String> names = conf.getStringList("names");
                    if (names == null) {
                        names = new ArrayList<>();
                    }
                    names.add(p.getName() + " - " + date);
                    conf.set("names", names);
                    conf.set("name", p.getName());
                    if (p.isOnline()) {
                        r.sendMes((CommandSender) p, "nameChanged", "%Oldname", oldname, "%Newname",
                                p.getName());
                    } else {
                        conf.set("oldname", oldname);
                    }
                    conf.save();
                }
            }
        }
    }
    if (request != null) {
        final ArrayList<UUID> req = request;
        try {
            r.log("Starting playerfile update...");
            HashMap<UUID, String> s = new UuidToName(req).call();
            for (UUID u : s.keySet()) {
                String n = s.get(u);
                File f = new File(
                        r.getUC().getDataFolder() + File.separator + "Players" + File.separator + u + ".yml");
                Config conf = new Config(f);
                conf.set("name", n);
                conf.save();
                //
                if (!conf.contains("names")) {
                    ArrayList<String> names = new ArrayList<>();
                    Calendar timeCal = Calendar.getInstance();
                    timeCal.setTimeInMillis(System.currentTimeMillis());
                    String date = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(timeCal.getTime());
                    names.add(n + " - " + date);
                    conf.set("names", names);
                    conf.save();
                }
                //
            }
            r.log("Playerfile update complete.");
        } catch (Exception e) {
            ErrorLogger.log(e, "Failed to convert uuids to names.");
        }
    }

}

From source file:bammerbom.ultimatecore.spongeapi.resources.utils.UuidUtil.java

public static void loadPlayers() {
    File directory = new File(r.getUC().getDataFolder() + File.separator + "Players");
    if (!directory.exists()) {
        directory.mkdirs();/*from w w  w.j a  va  2s  .c o  m*/
    }
    ArrayList<UUID> request = null;
    for (User p : r.getOfflinePlayers()) {
        if (p.getUniqueId() == null) {
            continue;
        }
        final File file = new File(r.getUC().getDataFolder() + File.separator + "Players" + File.separator
                + p.getUniqueId() + ".yml");
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                ErrorLogger.log(e, "Failed to create new file for " + p.getUniqueId());
            }
        }
        Config conf = new Config(file);
        if (p.getName() == null) {
            if (!conf.contains("name")) {
                if (request == null) {
                    request = new ArrayList<>();
                }
                request.add(p.getUniqueId());
            }
        } else {
            if (!conf.contains("name")) {
                conf.set("name", p.getName());
                conf.save();
                if (!conf.contains("names")) {
                    ArrayList<String> names = new ArrayList<>();
                    Calendar timeCal = Calendar.getInstance();
                    timeCal.setTimeInMillis(System.currentTimeMillis());
                    String date = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(timeCal.getTime());
                    names.add(p.getName() + " - " + date);
                    conf.set("names", names);
                    conf.save();
                }
            } else {
                if (!conf.contains("names")) {
                    ArrayList<String> names = new ArrayList<>();
                    Calendar timeCal = Calendar.getInstance();
                    timeCal.setTimeInMillis(System.currentTimeMillis());
                    String date = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(timeCal.getTime());
                    names.add(p.getName() + " - " + date);
                    conf.set("names", names);
                    conf.save();
                }
                if (!conf.getString("name").equals(p.getName())) {
                    String oldname = conf.getString("name");
                    Calendar timeCal = Calendar.getInstance();
                    timeCal.setTimeInMillis(System.currentTimeMillis());
                    String date = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(timeCal.getTime());
                    List<String> names = conf.getStringList("names");
                    if (names == null) {
                        names = new ArrayList<>();
                    }
                    names.add(p.getName() + " - " + date);
                    conf.set("names", names);
                    conf.set("name", p.getName());
                    if (p.isOnline()) {
                        r.sendMes((CommandSource) p, "nameChanged", "%Oldname", oldname, "%Newname",
                                p.getName());
                    } else {
                        conf.set("oldname", oldname);
                    }
                    conf.save();
                }
            }
        }
    }
    if (request != null) {
        final ArrayList<UUID> req = request;
        try {
            r.log("Starting playerfile update...");
            HashMap<UUID, String> s = new UuidToName(req).call();
            for (UUID u : s.keySet()) {
                String n = s.get(u);
                File f = new File(
                        r.getUC().getDataFolder() + File.separator + "Players" + File.separator + u + ".yml");
                Config conf = new Config(f);
                conf.set("name", n);
                conf.save();
                //
                if (!conf.contains("names")) {
                    ArrayList<String> names = new ArrayList<>();
                    Calendar timeCal = Calendar.getInstance();
                    timeCal.setTimeInMillis(System.currentTimeMillis());
                    String date = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(timeCal.getTime());
                    names.add(n + " - " + date);
                    conf.set("names", names);
                    conf.save();
                }
                //
            }
            r.log("Playerfile update complete.");
        } catch (Exception e) {
            ErrorLogger.log(e, "Failed to convert uuids to names.");
        }
    }

}

From source file:Main.java

static int dateDiff(int type, Calendar fromDate, Calendar toDate, boolean future) {
    int diff = 0;
    long savedDate = fromDate.getTimeInMillis();
    while ((future && !fromDate.after(toDate)) || (!future && !fromDate.before(toDate))) {
        savedDate = fromDate.getTimeInMillis();
        fromDate.add(type, future ? 1 : -1);
        diff++;/*from ww  w . ja v a 2  s.co m*/
    }
    diff--;
    fromDate.setTimeInMillis(savedDate);
    return diff;
}

From source file:jp.sonymusicstudio.cast.castcompanionlibrary.utils.Utils.java

/**
 * Builds and returns a {@link MediaInfo} that was wrapped in a {@link Bundle} by
 * <code>mediaInfoToBundle</code>. It is assumed that the type of the {@link MediaInfo} is
 * {@code MediaMetaData.MEDIA_TYPE_MOVIE}
 *
 * @see <code>mediaInfoToBundle()</code>
 *///from ww  w .  j  a va 2s .co  m
public static MediaInfo bundleToMediaInfo(Bundle wrapper) {
    if (wrapper == null) {
        return null;
    }

    MediaMetadata metaData = new MediaMetadata(wrapper.getInt(KEY_MEDIA_TYPE));

    metaData.putString(MediaMetadata.KEY_SUBTITLE, wrapper.getString(MediaMetadata.KEY_SUBTITLE));
    metaData.putString(MediaMetadata.KEY_TITLE, wrapper.getString(MediaMetadata.KEY_TITLE));
    metaData.putString(MediaMetadata.KEY_STUDIO, wrapper.getString(MediaMetadata.KEY_STUDIO));
    metaData.putString(MediaMetadata.KEY_ALBUM_ARTIST, wrapper.getString(MediaMetadata.KEY_ALBUM_ARTIST));
    metaData.putString(MediaMetadata.KEY_ALBUM_TITLE, wrapper.getString(MediaMetadata.KEY_ALBUM_TITLE));
    metaData.putString(MediaMetadata.KEY_COMPOSER, wrapper.getString(MediaMetadata.KEY_COMPOSER));
    metaData.putString(MediaMetadata.KEY_SERIES_TITLE, wrapper.getString(MediaMetadata.KEY_SERIES_TITLE));

    metaData.putInt(MediaMetadata.KEY_SEASON_NUMBER, wrapper.getInt(MediaMetadata.KEY_SEASON_NUMBER));
    metaData.putInt(MediaMetadata.KEY_EPISODE_NUMBER, wrapper.getInt(MediaMetadata.KEY_EPISODE_NUMBER));

    long releaseDateMillis = wrapper.getLong(MediaMetadata.KEY_RELEASE_DATE, 0);
    if (releaseDateMillis > 0) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(releaseDateMillis);
        metaData.putDate(MediaMetadata.KEY_RELEASE_DATE, calendar);
    }

    ArrayList<String> images = wrapper.getStringArrayList(KEY_IMAGES);
    if (images != null && !images.isEmpty()) {
        for (String url : images) {
            Uri uri = Uri.parse(url);
            metaData.addImage(new WebImage(uri));
        }
    }

    String customDataStr = wrapper.getString(KEY_CUSTOM_DATA);
    JSONObject customData = null;
    if (!TextUtils.isEmpty(customDataStr)) {
        try {
            customData = new JSONObject(customDataStr);
        } catch (JSONException e) {
            LOGE(TAG, "Failed to deserialize the custom data string: custom data= " + customDataStr);
        }
    }

    List<MediaTrack> mediaTracks = null;
    if (wrapper.getString(KEY_TRACKS_DATA) != null) {
        try {
            JSONArray jsonArray = new JSONArray(wrapper.getString(KEY_TRACKS_DATA));
            mediaTracks = new ArrayList<MediaTrack>();

            if (jsonArray.length() > 0) {
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObj = (JSONObject) jsonArray.get(i);
                    MediaTrack.Builder builder = new MediaTrack.Builder(jsonObj.getLong(KEY_TRACK_ID),
                            jsonObj.getInt(KEY_TRACK_TYPE));
                    if (jsonObj.has(KEY_TRACK_NAME)) {
                        builder.setName(jsonObj.getString(KEY_TRACK_NAME));
                    }
                    if (jsonObj.has(KEY_TRACK_SUBTYPE)) {
                        builder.setSubtype(jsonObj.getInt(KEY_TRACK_SUBTYPE));
                    }
                    if (jsonObj.has(KEY_TRACK_CONTENT_ID)) {
                        builder.setContentType(jsonObj.getString(KEY_TRACK_CONTENT_ID));
                    }
                    if (jsonObj.has(KEY_TRACK_LANGUAGE)) {
                        builder.setLanguage(jsonObj.getString(KEY_TRACK_LANGUAGE));
                    }
                    if (jsonObj.has(KEY_TRACKS_DATA)) {
                        builder.setCustomData(new JSONObject(jsonObj.getString(KEY_TRACKS_DATA)));
                    }
                    mediaTracks.add(builder.build());
                }
            }

        } catch (JSONException e) {
            LOGE(TAG, "Failed to build media tracks from the wrapper bundle", e);
        }
    }
    MediaInfo.Builder mediaBuilder = new MediaInfo.Builder(wrapper.getString(KEY_URL))
            .setStreamType(wrapper.getInt(KEY_STREAM_TYPE)).setContentType(wrapper.getString(KEY_CONTENT_TYPE))
            .setMetadata(metaData).setCustomData(customData).setMediaTracks(mediaTracks);

    if (wrapper.containsKey(KEY_STREAM_DURATION) && wrapper.getLong(KEY_STREAM_DURATION) >= 0) {
        mediaBuilder.setStreamDuration(wrapper.getLong(KEY_STREAM_DURATION));
    }

    return mediaBuilder.build();
}

From source file:Main.java

public static String getTimeMillisToYyyyMmDdDate(long milliSeconds) {
    Calendar cal = Calendar.getInstance();
    java.util.Date currentTime = cal.getTime();
    // Create a DateFormatter object for displaying date in specified format.
    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
    // String ndate = formatter.format(currentTime);
    // Create a calendar object that will convert the date and time value in milliseconds to date. 
    cal.setTimeInMillis(milliSeconds);
    return formatter.format(cal.getTime());
}