Java String to Time replaceDateTime(String input)

Here you can find the source of replaceDateTime(String input)

Description

* If the input String contains the sequence {date}pattern{date}, replaces the first occurrence of this sequence with a UTC date/time string formatted according to pattern.

License

Apache License

Parameter

Parameter Description
input The input String into which a formatted date/time will be inserted

Return

String

Declaration

public static String replaceDateTime(String input) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.SimpleTimeZone;

public class Main {
    /** ***************************************************************
     * If the input String contains the sequence {date}pattern{date},
     * replaces the first occurrence of this sequence with a UTC
     * date/time string formatted according to pattern.  If the input
     * String does not contain the squence, it is returned unaltered.
     */*www .j a v a2  s. com*/
     * @param input The input String into which a formatted date/time
     * will be inserted
     *
     * @return String
     */
    public static String replaceDateTime(String input) {

        String output = input;
        try {
            String token = "{date}";
            if (isNonEmptyString(output) && output.contains(token)) {
                int tlen = token.length();
                StringBuilder sb = new StringBuilder(output);
                int p1f = sb.indexOf(token);
                while (p1f > -1) {
                    int p1b = (p1f + tlen);
                    if (p1b < sb.length()) {
                        int p2f = sb.indexOf(token, p1b);
                        if (p2f > -1) {
                            String pattern = sb.substring(p1b, p2f);
                            int p2b = (p2f + tlen);
                            sb.replace(p1f, p2b, getDateTime(pattern));
                            p1f = sb.indexOf(token);
                        }
                    }
                }
                output = sb.toString();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return output;
    }

    /** ***************************************************************
     * @param obj Any object
     * @return true if obj is a non-empty String, else false.
     */
    public static boolean isNonEmptyString(Object obj) {

        return ((obj instanceof String) && !obj.equals(""));
    }

    /** ***************************************************************
     * Returns a date/time string corresponding to pattern.  The
     * date/time returned is the date/time of the method call.  The
     * locale is UTC (Greenwich).
     *
     * @param pattern Examples: yyyy, yyyy-MM-dd.
     *
     */
    public static String getDateTime(String pattern) {

        String dateTime = "";
        try {
            if (isNonEmptyString(pattern)) {
                SimpleDateFormat sdf = new SimpleDateFormat(pattern);
                sdf.setTimeZone(new SimpleTimeZone(0, "Greenwich"));
                dateTime = sdf.format(new Date());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return dateTime;
    }
}

Related

  1. getTime(String sDateTime, String eDateTime)
  2. getTime(String time)
  3. getTime(String time)
  4. getTimeDate(String time)
  5. getTimes(String startTime, String endTime)
  6. str2DateTime(String strDate)
  7. string2Date(String time)
  8. string2DateTime(String stringDate)
  9. string2DateTimeByAutoZero(String stringDate)