(adsbygoogle = window.adsbygoogle || []).push({}); O_o :: 'Java' 태그의 글 목록

'Java'에 해당되는 글 2건

  1. 2008.10.23 [Java] DataFormat Util with Time zone
  2. 2008.10.23 [Java] 정규 표현식 패턴

[Java] DataFormat Util with Time zone

|

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */
package org.apache.commons.lang.time;

import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * <p>Date and time formatting utilities and constants.</p>
 *
 * <p>Formatting is performed using the
 * {@link org.apache.commons.lang.time.FastDateFormat} class.</p>
 *
 * @author Apache Ant - DateUtils
 * @author <a href="mailto:sbailliez@apache.org">Stephane Bailliez</a>
 * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
 * @author Stephen Colebourne
 * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
 * @since 2.0
 * @version $Id: DateFormatUtils.java 437554 2006-08-28 06:21:41Z bayard $
 */
public class DateFormatUtils {

    /**
     * ISO8601 formatter for date-time without time zone.
     * The format used is <tt>yyyy-MM-dd'T'HH:mm:ss</tt>.
     */
    public static final FastDateFormat ISO_DATETIME_FORMAT
            = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss");

    /**
     * ISO8601 formatter for date-time with time zone.
     * The format used is <tt>yyyy-MM-dd'T'HH:mm:ssZZ</tt>.
     */
    public static final FastDateFormat ISO_DATETIME_TIME_ZONE_FORMAT
            = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ssZZ");

    /**
     * ISO8601 formatter for date without time zone.
     * The format used is <tt>yyyy-MM-dd</tt>.
     */
    public static final FastDateFormat ISO_DATE_FORMAT
            = FastDateFormat.getInstance("yyyy-MM-dd");

    /**
     * ISO8601-like formatter for date with time zone.
     * The format used is <tt>yyyy-MM-ddZZ</tt>.
     * This pattern does not comply with the formal ISO8601 specification
     * as the standard does not allow a time zone  without a time.
     */
    public static final FastDateFormat ISO_DATE_TIME_ZONE_FORMAT
            = FastDateFormat.getInstance("yyyy-MM-ddZZ");

    /**
     * ISO8601 formatter for time without time zone.
     * The format used is <tt>'T'HH:mm:ss</tt>.
     */
    public static final FastDateFormat ISO_TIME_FORMAT
            = FastDateFormat.getInstance("'T'HH:mm:ss");

    /**
     * ISO8601 formatter for time with time zone.
     * The format used is <tt>'T'HH:mm:ssZZ</tt>.
     */
    public static final FastDateFormat ISO_TIME_TIME_ZONE_FORMAT
            = FastDateFormat.getInstance("'T'HH:mm:ssZZ");

    /**
     * ISO8601-like formatter for time without time zone.
     * The format used is <tt>HH:mm:ss</tt>.
     * This pattern does not comply with the formal ISO8601 specification
     * as the standard requires the 'T' prefix for times.
     */
    public static final FastDateFormat ISO_TIME_NO_T_FORMAT
            = FastDateFormat.getInstance("HH:mm:ss");

    /**
     * ISO8601-like formatter for time with time zone.
     * The format used is <tt>HH:mm:ssZZ</tt>.
     * This pattern does not comply with the formal ISO8601 specification
     * as the standard requires the 'T' prefix for times.
     */
    public static final FastDateFormat ISO_TIME_NO_T_TIME_ZONE_FORMAT
            = FastDateFormat.getInstance("HH:mm:ssZZ");

    /**
     * SMTP (and probably other) date headers.
     * The format used is <tt>EEE, dd MMM yyyy HH:mm:ss Z</tt> in US locale.
     */
    public static final FastDateFormat SMTP_DATETIME_FORMAT
            = FastDateFormat.getInstance("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US);

    //-----------------------------------------------------------------------
    /**
     * <p>DateFormatUtils instances should NOT be constructed in standard programming.</p>
     *
     * <p>This constructor is public to permit tools that require a JavaBean instance
     * to operate.</p>
     */
    public DateFormatUtils() {
        super();
    }

    /**
     * <p>Formats a date/time into a specific pattern using the UTC time zone.</p>
     *
     * @param millis  the date to format expressed in milliseconds
     * @param pattern  the pattern to use to format the date
     * @return the formatted date
     */
    public static String formatUTC(long millis, String pattern) {
        return format(new Date(millis), pattern, DateUtils.UTC_TIME_ZONE, null);
    }

    /**
     * <p>Formats a date/time into a specific pattern using the UTC time zone.</p>
     *
     * @param date  the date to format
     * @param pattern  the pattern to use to format the date
     * @return the formatted date
     */
    public static String formatUTC(Date date, String pattern) {
        return format(date, pattern, DateUtils.UTC_TIME_ZONE, null);
    }
   
    /**
     * <p>Formats a date/time into a specific pattern using the UTC time zone.</p>
     *
     * @param millis  the date to format expressed in milliseconds
     * @param pattern  the pattern to use to format the date
     * @param locale  the locale to use, may be <code>null</code>
     * @return the formatted date
     */
    public static String formatUTC(long millis, String pattern, Locale locale) {
        return format(new Date(millis), pattern, DateUtils.UTC_TIME_ZONE, locale);
    }

    /**
     * <p>Formats a date/time into a specific pattern using the UTC time zone.</p>
     *
     * @param date  the date to format
     * @param pattern  the pattern to use to format the date
     * @param locale  the locale to use, may be <code>null</code>
     * @return the formatted date
     */
    public static String formatUTC(Date date, String pattern, Locale locale) {
        return format(date, pattern, DateUtils.UTC_TIME_ZONE, locale);
    }
   
    /**
     * <p>Formats a date/time into a specific pattern.</p>
     *
     * @param millis  the date to format expressed in milliseconds
     * @param pattern  the pattern to use to format the date
     * @return the formatted date
     */
    public static String format(long millis, String pattern) {
        return format(new Date(millis), pattern, null, null);
    }

    /**
     * <p>Formats a date/time into a specific pattern.</p>
     *
     * @param date  the date to format
     * @param pattern  the pattern to use to format the date
     * @return the formatted date
     */
    public static String format(Date date, String pattern) {
        return format(date, pattern, null, null);
    }
   
    /**
     * <p>Formats a date/time into a specific pattern in a time zone.</p>
     *
     * @param millis  the time expressed in milliseconds
     * @param pattern  the pattern to use to format the date
     * @param timeZone  the time zone  to use, may be <code>null</code>
     * @return the formatted date
     */
    public static String format(long millis, String pattern, TimeZone timeZone) {
        return format(new Date(millis), pattern, timeZone, null);
    }

    /**
     * <p>Formats a date/time into a specific pattern in a time zone.</p>
     *
     * @param date  the date to format
     * @param pattern  the pattern to use to format the date
     * @param timeZone  the time zone  to use, may be <code>null</code>
     * @return the formatted date
     */
    public static String format(Date date, String pattern, TimeZone timeZone) {
        return format(date, pattern, timeZone, null);
    }

    /**
     * <p>Formats a date/time into a specific pattern in a locale.</p>
     *
     * @param millis  the date to format expressed in milliseconds
     * @param pattern  the pattern to use to format the date
     * @param locale  the locale to use, may be <code>null</code>
     * @return the formatted date
     */
    public static String format(long millis, String pattern, Locale locale) {
        return format(new Date(millis), pattern, null, locale);
    }

    /**
     * <p>Formats a date/time into a specific pattern in a locale.</p>
     *
     * @param date  the date to format
     * @param pattern  the pattern to use to format the date
     * @param locale  the locale to use, may be <code>null</code>
     * @return the formatted date
     */
    public static String format(Date date, String pattern, Locale locale) {
        return format(date, pattern, null, locale);
    }

    /**
     * <p>Formats a date/time into a specific pattern in a time zone  and locale.</p>
     *
     * @param millis  the date to format expressed in milliseconds
     * @param pattern  the pattern to use to format the date
     * @param timeZone  the time zone  to use, may be <code>null</code>
     * @param locale  the locale to use, may be <code>null</code>
     * @return the formatted date
     */
    public static String format(long millis, String pattern, TimeZone timeZone, Locale locale) {
        return format(new Date(millis), pattern, timeZone, locale);
    }

    /**
     * <p>Formats a date/time into a specific pattern in a time zone  and locale.</p>
     *
     * @param date  the date to format
     * @param pattern  the pattern to use to format the date
     * @param timeZone  the time zone  to use, may be <code>null</code>
     * @param locale  the locale to use, may be <code>null</code>
     * @return the formatted date
     */
    public static String format(Date date, String pattern, TimeZone timeZone, Locale locale) {
        FastDateFormat df = FastDateFormat.getInstance(pattern, timeZone, locale);
        return df.format(date);
    }

}

And

[Java] 정규 표현식 패턴

|

/abc/
이 패턴은 'abc'라는 문자와 순서가 모두 일치하는 한다는 의미입니다.
"Hi, my name is abcd."
이 문장에서 붉은 색 부분이 패턴과 일치하는 부분입니다. 'abc'는 일치하지만 'acb', 'ab c'는 일치하지 않습니다.

정규표현식에서는 특수문자를 사용해서 다양한 패턴을 조합할 수 있습니다. 이제부터는 정규표현식의 특수문자들에 대해서 하나씩 살펴볼까 합니다.

?
뒤의 문자가 이스케잎 문자로 사용됩니다.
/b/ : b와 일치.
/?b/ : 단어의 경계.
/a*/ : a가 0개 이상.
/a?*/ : a*와 일치.

^
입력의 시작 위치를 의미합니다. 여러 줄 모드에서는 줄바꿈 뒤도 의미합니다.
/^A/
"An A"에서 시작 위치 바로 뒤의 A는 일치하지만 마지막의 A는 일치하지 않습니다.

$
입력의 마지막 위치를 의미합니다. 여러 줄 모드에서는 줄바꿈 앞도 의미합니다.
/a$/
"Cocoa"에서 마지막에 있는 'a'와 일치합니다.

*
* 앞의 문자가 0번 이상 반복됨을 의미합니다.
/ab*/
'a', 'ab', 'abbbbbbbbb' 등이 일치합니다.

+
+ 앞의 문자가 1번 이상 반복됩니다. {1,}과 같은 의미입니다.
/a+/
'a', 'aaaaa' 등이 일치합니다.

?
? 앞의 문자가 0 또는 1번 반복됨을 의미합니다.
/a?b/
'b' 또는 'ab'가 됩니다.

.
아무 문자 하나를 의미합니다. 뉴라인 문자는 제외합니다.
/h.t/
'hot', 'hat' 등이 해당될 수 있지만, 'heat'는 안됩니다.

(x)
괄호로 묶여진 부분을 찾고 기억합니다. 결과 배열의 요소에 담겨집니다. (1 ... n). 배열의 첫번째 요소(0)에는 일치하는 전체 텍스트가 담깁니다.
/a(b+)/

(?:x)
괄호내의 부분을 찾지만 기억하지 않습니다.
/a(?:b+)/

x(?=y)
x 뒤에 y가 따라오는 x를 의미합니다. 괄호 안의 부분은 일치 결과에서 제외됩니다.
/?d+(?=?.)/
"3.14"라는 문자열에서 '3'과 일치합니다.

x(?!y)
x 뒤에 y가 따라오지 않는 x를 의미합니다.
/?d+(?!?.)/
"3.14"라는 문자열에서 '14'와 일치합니다.

x|y
x 또는 y와 일치합니다.
/red|green/
"red apple", "green apple"에서 'red', 'green'모두 일치됩니다.

{n}
앞에 나온 문자가 정확히 n번 반복됩니다. n은 양의 정수 값을 입력합니다.
/a{3}/
일치하는 'aaa'를 찾습니다.

{n,}
앞의 문자가 최소 n번 이상 반복됩니다. n은 양의 정수 값을 입력합니다.
/a{3,}/
'aaa', 'aaaa' 등이 해당됩니다.

{n,m}
앞의 문자가 최소 n번 이상, 최대 m번 이하 반복됩니다. n은 양의 정수 값을 입력합니다.
/ca{2,4}/
'caa', 'caaa', 'caaaa'가 해당됩니다.

[xyz]
[ ] 안의 문자 집합 중 어느 하나에 해당되는 문자입니다. 연속된 범위를 나타내기 위해서는 -를 사용합니다.
/[abcd]/
"handy"에서 'a', 'd'가 해당됩니다.
/[abcd]/는 /[a-d]/와 같습니다.

[^xyz]
[ ] 안의 문자 집합 중 어느 하나에도 해당하지 않는 문자를 의미합니다. 연속된 범위를 나타내기 위해서는 -를 사용합니다.
/[^abcd]/
"handy"에서는 'h', 'n', 'y'가 해당됩니다.

[?b]
백스페이스를 의미합니다. ?b와 혼동하지 않아야 합니다.

?b
공백 또는 줄바꿈 등 단어의 경계를 의미합니다.
/?bn?w/
"It's nooonday"의 'no'는 해당되지만 단어의 중간에 나오는 'nd'는 해당되지 않습니다.

?B
단어의 경계가 아님을 의미합니다.
/y?B?w/
"lovely yesterday"에서 y의 뒤에 단어의 경계가 없는 'ye'가 해당됩니다.

?cX
제어문자를 표현합니다.
/?cM/
Ctrl-M을 나타냅니다.

?d
숫자 하나를 의미합니다. [0-9]와 같은 의미입니다.
/?d/
"B2"에서 '2'만 해당됩니다.

?D
숫자가 아닌 문자를 의미합니다.[^0-9]와 의미가 같습니다.
/?D/
"B2"에서 'B'가 해당됩니다.

?f
폼 피드를 의미합니다.

?n
라인 피드를 의미합니다.

?r
캐리지 리턴을 의미합니다.

?s
공백문자 하나를 의미합니다. 스페이스, 탭, 폼피드, 라인피드 등. [ ?f?n?r?t?v?u00A0?u2028?u2029]와 같은 의미입니다.

?S
공백이 아닌 문자 하나를 의미합니다. [^ ?f?n?r?t?v?u00A0?u2028?u2029]와 같은 의미입니다.

?t
탭 문자를 의미합니다.

?v
버티컬 탭을 의미합니다.

?w
알파벳, 숫자, 밑줄 중 어느 하나의 문자를 의미합니다. [A-Za-z0-9_]와 같은 의미입니다.

?W
알파벳, 숫자, 밑줄 중 어느 하나도 아닌 문자를 의미합니다. [^A-Za-z0-9_]와 같은 의미입니다.

?n
역참조에 사용됩니다. 패턴에서 n번째 괄호로 묶인 부분 텍스트를 참조합니다. n은 양의 정수입니다.
/a(,)?sb?1/
"a, b, c, d, e"에서 'a, b,'가 해당됩니다.

?0
널문자를 의미합니다. 0뒤에 숫자가 붙어서는 안됩니다.

?xhh
두자리 16진 코드에 해당하는 문자를 나타냅니다.

?uhhhh
네자리 16진 코드에 해당하는 문자를 나타냅니다.

And
prev | 1 | next