Example usage for java.text SimpleDateFormat format

List of usage examples for java.text SimpleDateFormat format

Introduction

In this page you can find the example usage for java.text SimpleDateFormat format.

Prototype

public final String format(Date date) 

Source Link

Document

Formats a Date into a date-time string.

Usage

From source file:MainClass.java

public static void main(String[] args) throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
    GregorianCalendar gc = new GregorianCalendar();
    java.util.Date d = sdf.parse("12/12/2003");
    gc.setTime(d);// ww  w. jav  a  2 s  .  co m
    System.out.println("Input Date = " + sdf.format(d));
    int dayBefore = gc.get(Calendar.DAY_OF_YEAR);
    gc.roll(Calendar.DAY_OF_YEAR, -1);
    int dayAfter = gc.get(Calendar.DAY_OF_YEAR);
    if (dayAfter > dayBefore) {
        gc.roll(Calendar.YEAR, -1);
    }
    gc.get(Calendar.DATE);
    java.util.Date yesterday = gc.getTime();
    System.out.println("Yesterdays Date = " + sdf.format(yesterday));

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String dStr = "Wed, 05 Jun 2015 00:48:12 GMT";
    SimpleDateFormat ft = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
    Date t = ft.parse(dStr);//ww w . j ava  2 s .c  o m
    TimeZone gmt = TimeZone.getTimeZone("England/London");
    ft.setTimeZone(gmt);
    System.out.println(t);
    System.out.println(ft.format(t));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    SimpleDateFormat sd = new SimpleDateFormat();
    String dateString = "2015-01-02 14:59:27.953";
    sd.applyPattern("yyyy-MM-dd HH:mm:ss.SSS");
    Date date = sd.parse(dateString);
    sd.applyPattern("MM/dd/yyyy HH:mm a");
    System.out.println(sd.format(date));
}

From source file:com.webkruscht.wmt.DownloadFiles.java

/**
 * @param args/*from   w  w w .  j  a v  a 2  s.  c  o  m*/
 * @throws Exception 
 */
public static void main(String[] args) throws Exception {
    WebmasterTools wmt;
    String filename;
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    String today = sdf.format(date);
    getProperties();
    Options options = getOptions(args);

    try {
        wmt = new WebmasterTools(username, password);

        for (SitesEntry entry : wmt.getUserSites()) {
            // only process verified sites
            if (entry.getVerified()) {
                // get download paths for site
                JSONObject data = wmt.getDownloadList(entry);
                if (data != null) {
                    for (String prop : props) {
                        String path = (String) data.get("TOP_QUERIES");
                        path += "&prop=" + prop;
                        URL url = new URL(entry.getTitle().getPlainText());
                        if (options.getStartdate() != null) {
                            path += "&db=" + options.getStartdate();
                            path += "&de=" + options.getEnddate();
                            filename = String.format("%s-%s-%s-%s-%s.csv", url.getHost(),
                                    options.getStartdate(), options.getEnddate(), prop, "TopQueries");
                        } else {
                            filename = String.format("%s-%s-%s-%s.csv", url.getHost(), today, prop,
                                    "TopQueries");
                        }
                        OutputStreamWriter out = new OutputStreamWriter(
                                new FileOutputStream(filePath + filename), "UTF-8");
                        wmt.downloadData(path, out);
                        out.close();
                    }
                    String path = (String) data.get("TOP_PAGES");
                    URL url = new URL(entry.getTitle().getPlainText());
                    filename = String.format("%s-%s-%s.csv", url.getHost(), today, "TopQueries");
                    OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(filePath + filename),
                            "UTF-8");
                    wmt.downloadData(path, out);
                    out.close();
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }

}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    SimpleDateFormat sdf = new SimpleDateFormat();
    DateFormatSymbols dfs = sdf.getDateFormatSymbols();

    String era[] = { "BCE", "CE" };
    dfs.setEras(era);//w w w.  ja  v  a  2  s  . c om
    sdf.setDateFormatSymbols(dfs);

    sdf.applyPattern("MMMM d yyyy G");
    System.out.println(sdf.format(new Date()));
}

From source file:ChangeEra.java

public static void main(String s[]) {
    SimpleDateFormat sdf = new SimpleDateFormat();
    DateFormatSymbols dfs = sdf.getDateFormatSymbols();

    String era[] = { "BCE", "CE" };
    dfs.setEras(era);// w w w  . j av a  2  s .  c  o  m
    sdf.setDateFormatSymbols(dfs);

    sdf.applyPattern("MMMM d yyyy G");
    System.out.println(sdf.format(new Date()));
}

From source file:Main.java

public static void main(String[] args) {
    String maxDate = "15012009";
    SimpleDateFormat fromFormat = new SimpleDateFormat("ddMMyyyy");
    SimpleDateFormat toFormat = new SimpleDateFormat("MMMM dd, yyyy");
    Date date = null;/*from   w  w  w.j av  a 2s . co  m*/
    try {
        date = fromFormat.parse(maxDate);
    } catch (java.text.ParseException e) {
        e.printStackTrace();
    }
    System.out.println("formated date:-" + toFormat.format(date));
}

From source file:MainClass.java

public static void main(String[] args) {
    String pattern = "MM/dd/yyyy";
    SimpleDateFormat format = new SimpleDateFormat(pattern);
    try {/*w  ww. j av a  2 s . c  o  m*/
        Date date = format.parse("12/31/2006");
        System.out.println(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    // formatting
    System.out.println(format.format(new Date()));
}

From source file:tasly.greathealth.jd.order.service.impl.JdCreateOrderServiceImpl.java

public static void main(final String[] args) throws JdException {
    final Date d = new Date();
    final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    System.out.println("" + df.format(d));
    System.out.println("?" + df.format(new Date(d.getTime() - 2 * 24 * 60 * 60 * 1000)));
    System.out.println("?" + df.format(new Date(d.getTime() + 3 * 24 * 60 * 60 * 1000)));

    final String startExecuttime = TaslyThirdUtils.formatTime(d);
    final String endExecuttime = TaslyThirdUtils.addTime(d, 5);
    System.out.println("startExecuttime:" + startExecuttime + ",endExecuttime:" + endExecuttime);

}

From source file:io.minimum.minecraft.rbclean.RedisBungeeClean.java

public static void main(String... args) {
    Options options = new Options();

    Option hostOption = new Option("h", "host", true, "Sets the Redis host to use.");
    hostOption.setRequired(true);// ww  w .  java2  s .c  om
    options.addOption(hostOption);

    Option portOption = new Option("p", "port", true, "Sets the Redis port to use.");
    options.addOption(portOption);

    Option passwordOption = new Option("w", "password", true, "Sets the Redis password to use.");
    options.addOption(passwordOption);

    Option dryRunOption = new Option("d", "dry-run", false, "Performs a dry run (no data is modified).");
    options.addOption(dryRunOption);

    CommandLine commandLine;

    try {
        commandLine = new DefaultParser().parse(options, args);
    } catch (ParseException e) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("RedisBungeeClean", options);
        return;
    }

    int port = commandLine.hasOption('p') ? Integer.parseInt(commandLine.getOptionValue('p')) : 6379;

    try (Jedis jedis = new Jedis(commandLine.getOptionValue('h'), port, 0)) {
        if (commandLine.hasOption('w')) {
            jedis.auth(commandLine.getOptionValue('w'));
        }

        System.out.println("Fetching UUID cache...");
        Map<String, String> uuidCache = jedis.hgetAll("uuid-cache");
        Gson gson = new Gson();

        // Just in case we need it, compress everything in JSON format.
        if (!commandLine.hasOption('d')) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");
            File file = new File("uuid-cache-previous-" + dateFormat.format(new Date()) + ".json.gz");
            try {
                file.createNewFile();
            } catch (IOException e) {
                System.out.println("Can't write backup of the UUID cache, will NOT proceed.");
                e.printStackTrace();
                return;
            }

            System.out.println("Creating backup (as " + file.getName() + ")...");

            try (OutputStreamWriter bw = new OutputStreamWriter(
                    new GZIPOutputStream(new FileOutputStream(file)))) {
                gson.toJson(uuidCache, bw);
            } catch (IOException e) {
                System.out.println("Can't write backup of the UUID cache, will NOT proceed.");
                e.printStackTrace();
                return;
            }
        }

        System.out.println("Cleaning out the bird cage (this may take a while...)");
        int originalSize = uuidCache.size();
        for (Iterator<Map.Entry<String, String>> it = uuidCache.entrySet().iterator(); it.hasNext();) {
            CachedUUIDEntry entry = gson.fromJson(it.next().getValue(), CachedUUIDEntry.class);

            if (entry.expired()) {
                it.remove();
            }
        }
        int newSize = uuidCache.size();

        if (commandLine.hasOption('d')) {
            System.out.println(
                    (originalSize - newSize) + " records would be expunged if a dry run was not conducted.");
        } else {
            System.out.println("Expunging " + (originalSize - newSize) + " records...");
            Transaction transaction = jedis.multi();
            transaction.del("uuid-cache");
            transaction.hmset("uuid-cache", uuidCache);
            transaction.exec();
            System.out.println("Expunging complete.");
        }
    }
}