Java Parse Date Pattern YYYY parseToRegExcel(String raw)

Here you can find the source of parseToRegExcel(String raw)

Description

parse To Reg Excel

License

Apache License

Declaration

public static String parseToRegExcel(String raw) 

Method Source Code

//package com.java2s;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 * Copyright 2013 Joseph Yuan/*w ww  .j  a v  a2  s . com*/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static String parseToRegExcel(String raw) {
        char c;
        String buf = "", regExcel = "";
        for (int i = 0; i < raw.length(); i++) {
            c = raw.charAt(i);
            if (isNumeric(c) && i + 1 < raw.length()
                    && isNumeric(raw.charAt(i + 1))) {
                while (isNumeric(c)) {
                    buf += c;
                    i++;
                    c = raw.charAt(i);
                }
                regExcel += parseRegExcelNumber(buf) + c;
                buf = "";
            } else {
                regExcel += c;
            }
        }
        return regExcel;
    }

    public static boolean isNumeric(String str) {
        String s = str;
        if (s.startsWith("-")) {
            s = s.substring(1);
        }
        char c;
        int i, L = s.length(), sinceLastComma = 0;
        boolean decimalHit = false, commaHit = false;
        for (i = 0; i < L; i++) {
            c = s.charAt(i);
            if (c < '0' || c > '9') {
                if (c == '.' && !decimalHit) {
                    decimalHit = true;
                    if (commaHit && sinceLastComma != 3) {
                        return false;
                    }
                    continue;
                } else if (c == ',' && !decimalHit) {
                    if (commaHit) {
                        if (sinceLastComma != 3) {
                            return false;
                        }
                        sinceLastComma = 0;
                    }
                    commaHit = true;
                    continue;
                }
                return false;
            } else {
                if (commaHit)
                    sinceLastComma++;
            }
        }
        return true;
    }

    public static boolean isNumeric(char c) {
        return '0' <= c && '9' >= c;
    }

    private static String parseRegExcelNumber(String number) {
        String today_d = formatToday("dd"), today_m = formatToday("MM"), today_y = formatToday("yyyy");
        String regex = "(" + today_y.substring(0, 2) + ")?"
                + today_y.substring(2) + today_m + today_d;
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(number);
        String buf = "";
        if (m.find()) {
            if (m.group().length() == 6) {
                buf = (m.start() == 0 ? "" : "<NUMBER>")
                        + "<DATE:TODAY:YYMMDD>"
                        + (m.end() == number.length() ? "" : "<NUMBER>");
            } else {
                buf = (m.start() == 0 ? "" : "<NUMBER>")
                        + "<DATE:TODAY:YYYYMMDD>"
                        + (m.end() == number.length() ? "" : "<NUMBER>");
            }
            return buf;
        }
        String yesterday_d = formatYesterday("dd"), yesterday_m = formatYesterday("MM"), yesterday_y = formatYesterday("yyyy");
        regex = "(" + yesterday_y.substring(0, 2) + ")?"
                + yesterday_y.substring(2) + yesterday_m + yesterday_d;
        p = Pattern.compile(regex);
        m = p.matcher(number);
        buf = "";
        if (m.find()) {
            if (m.group().length() == 6) {
                buf = (m.start() == 0 ? "" : "<NUMBER>")
                        + "<DATE:YESTERDAY:YYMMDD>"
                        + (m.end() == number.length() ? "" : "<NUMBER>");
            } else {
                buf = (m.start() == 0 ? "" : "<NUMBER>")
                        + "<DATE:YESTERDAY:YYYYMMDD>"
                        + (m.end() == number.length() ? "" : "<NUMBER>");
            }
            return buf;
        }
        return "<NUMBER>";

    }

    public static String formatToday() {
        return formatDate("MM.dd.yy", 0, 0, 0);
    }

    public static String formatToday(String format) {
        return formatDate(format, 0, 0, 0);
    }

    public static String formatYesterday() {
        return formatDate("MM.dd.yy", 0, 0, -1);
    }

    public static String formatYesterday(String format) {
        return formatDate(format, 0, 0, -1);
    }

    public static String formatDate(String format, int offset_years,
            int offset_months, int offset_days) {
        Calendar date = Calendar.getInstance();
        date.add(Calendar.YEAR, offset_years);
        date.add(Calendar.MONTH, offset_months);
        date.add(Calendar.DATE, offset_days);
        return formatDate(format, date);
    }

    public static String formatDate(Calendar c) {
        return (new SimpleDateFormat("MM.dd.yy")).format(c.getTime());
    }

    public static String formatDate(String format, Calendar c) {
        return (new SimpleDateFormat(format)).format(c.getTime());
    }
}

Related

  1. parseToDate(final String date, final String format)
  2. parseToDate(String date)
  3. parseToDate(String date, String format)
  4. parseToDateLenient(final String date, final String format, final boolean lenient)
  5. parseToGregorianCalendar(String dateString)
  6. parseToString(Date date)
  7. parseToString(Date date, String format)
  8. parseToString(final Date date, final String format)
  9. parseToStringDate(long ms)