Example usage for org.apache.commons.lang3.time DateFormatUtils format

List of usage examples for org.apache.commons.lang3.time DateFormatUtils format

Introduction

In this page you can find the example usage for org.apache.commons.lang3.time DateFormatUtils format.

Prototype

public static String format(final Calendar calendar, final String pattern) 

Source Link

Document

Formats a calendar into a specific pattern.

Usage

From source file:org.manalith.ircbot.plugin.tweetreader.TweetReader.java

private String getText(String twitterurl, UrlType type) {
    if (type == null)
        return null;

    // init twitter4j
    try {//from  ww  w.jav  a 2  s  . c  om
        initTwitter4j();
    } catch (TwitterException e) {
        return e.getMessage();
    }

    if (StringUtils.isEmpty(config.getString("com.twitter.accessKey"))
            || StringUtils.isEmpty(config.getString("com.twitter.accessSecret"))) {
        try {
            authorizeTwitter();
        } catch (TwitterException e) {
            return "[twitter4j.TwitterException] " + e.getMessage();
        } catch (ConfigurationException e) {
            return "[org.apache.common.configuration.ConfigutaionException] " + e.getMessage();
        }
    } else {
        String accessToken = config.getString("com.twitter.accessKey");
        String accessSecret = config.getString("com.twitter.accessSecret");

        setAcecssToken(accessToken, accessSecret);
    }

    Status stat = null;
    try {
        stat = getStatus(twitterurl, type);
    } catch (TwitterException e) {
        return "[twitter4j.TwitterException] " + e.getMessage();
    }

    if (stat == null)
        return null;

    String author = stat.getUser().getName();
    String createdAt = DateFormatUtils.format(stat.getCreatedAt(), TARGET_DATE_PATTERN);
    String message = stat.getText();

    return String.format("?: %s, ?: %s, : %s", author, createdAt, message);
}

From source file:org.nanoframework.commons.format.ClassCastTest.java

@Test
public void castByObjectTest() {
    Object val = ClassCast.cast(new BigDecimal(1), Integer.class.getName());
    assertEquals(1, val);

    val = ClassCast.cast(new BigDecimal(1), Long.class.getName());
    assertEquals(1L, val);

    val = ClassCast.cast(new BigDecimal(1), Double.class.getName());
    assertEquals(1D, val);

    val = ClassCast.cast(new BigDecimal(1), Float.class.getName());
    assertEquals(1F, val);

    Date now = new Timestamp(System.currentTimeMillis());
    val = ClassCast.cast(DateFormatUtils.format(now, Pattern.TIMESTAMP.get()), Timestamp.class.getName());
    assertEquals(now.getTime(), ((Timestamp) val).getTime());
}

From source file:org.openhab.binding.IEEE1888.server.FIAPServer.java

private void sendPointsData(Point[] points) throws ItemNotFoundException {
    if (ArrayUtils.isNotEmpty(points)) {
        for (Point point : points) {
            // [value]
            Value[] value = point.getValue();
            if (ArrayUtils.isEmpty(value)) {
                continue;
            }/*  w w w  .  ja  v a2s .  com*/

            String itemName = buildItemName(point.getId());

            for (Value v : value) {
                String valueStr = v.getString();
                Calendar valueTime = v.getTime();
                Item item = IEEE1888Binding.getItemRegistry().getItem(itemName);
                String oldItemValue = item.getState().toString();

                // send command
                String valueTimeStr = DateFormatUtils.format(valueTime, TIME_FORMAT);

                List<String> buildCmd = new ArrayList<>();
                buildCmd.add("time=" + valueTimeStr);
                buildCmd.add("value=" + valueStr);
                String cmdStr = StringUtils.join(buildCmd, "&");

                Command cmd = TypeParser.parseCommand(item.getAcceptedCommandTypes(), cmdStr);
                if (cmd != null) {
                    IEEE1888Binding.getEventPublisher().postCommand(itemName, cmd);
                    logger.debug("command from FIAP service Binding: \n" + "itemName : " + itemName
                            + "\n cmd : " + cmd.toString());
                }

                if (oldItemValue != null) {
                    // analyze state
                    Matcher matcher = STATE_CONFIG_PATTERN.matcher(oldItemValue);
                    // update state
                    if (matcher.find()) {
                        valueStr = cmdStr;
                    }
                }
                State state = TypeParser.parseState(item.getAcceptedDataTypes(), valueStr);
                // following five lines may not be needed.
                if (state != null) {
                    IEEE1888Binding.getEventPublisher().postUpdate(itemName, state);
                    logger.debug("state from FIAP service Binding: \n" + "itemName : " + itemName + "\n cmd : "
                            + state.toString());
                }
            }
        }
    }
}

From source file:org.ourbeehive.mbp.io.FileHandler.java

public static String backup(String origFileName) throws IOException {

    // Get backup file name by inserting timestamp before extension name.
    String timestamp = DateFormatUtils.format(Calendar.getInstance().getTime(), "[yyyy.MM.dd'T'HH.mm.ss]");
    int lastIdxOfDot = origFileName.lastIndexOf(".");
    String backupFileName = origFileName.substring(0, lastIdxOfDot + 1) + timestamp
            + origFileName.substring(lastIdxOfDot, origFileName.length());

    // Create backup file by renaming original file.
    File origFile = new File(origFileName);
    File backFile = new File(backupFileName);

    logger.info("BACKUP: Begin to make a backup for file: " + origFileName);
    FileUtils.copyFile(origFile, backFile);

    return backupFileName;

}

From source file:org.power.commons.lang.util.time.DateUtils.java

/**
 * #func ?<br>/*  w  w  w.  j  a  v a  2s  . c om*/
 * #desc 
 *
 * @author hedan
 * @version 4.0
 */
public static String format(Date date, String pattern) {
    if (date == null) {
        return null;
    }
    return DateFormatUtils.format(date, pattern);
}

From source file:org.power.commons.lang.util.time.DateUtils.java

/**
 * #func ?<br>//w w w  .j a v a  2  s  .  c  o  m
 * #desc yyyy-MM-dd?
 *
 * @author hedan
 * @version VERSION
 */
public static String format(Date date) {
    return DateFormatUtils.format(date, DSP_DEFAULT_TIME_FORMAT);
}

From source file:org.repodriller.RepositoryMining.java

private void processChangeSet(SCMRepository repo, ChangeSet cs) {
    Commit commit = repo.getScm().getCommit(cs.getId());
    log.info("Commit #" + commit.getHash() + " @ " + repo.getLastDir() + " in "
            + DateFormatUtils.format(commit.getDate().getTime(), DATE_FORMAT) + " from "
            + commit.getAuthor().getName() + " with " + commit.getModifications().size() + " modifications");

    if (!filtersAccept(commit)) {
        log.info("-> Filtered");
        return;//w w  w. j  a v  a  2 s. c o m
    }

    visitors.processCommit(repo, commit);

}

From source file:org.rippleosi.common.util.DateFormatter.java

public static String toString(Date input) {
    if (input == null) {
        return null;
    }//  w w w  . j a va  2s  .  com

    return DateFormatUtils.format(input, "yyyy-MM-dd'T'HH:mm:ss.SSSZ");
}

From source file:org.rippleosi.common.util.DateFormatter.java

public static String toJSONDateString(Date input) {
    if (input == null) {
        return null;
    }/*from w  w w  . j  av  a  2 s . c  o  m*/

    return DateFormatUtils.format(input, "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
}

From source file:org.rippleosi.common.util.DateFormatter.java

public static String toSimpleDateString(Date input) {
    if (input == null) {
        return null;
    }//from   ww w.java2  s  . co m

    return DateFormatUtils.format(input, "yyyy-MM-dd");
}