package org.ctrip.ops.sysdev.utils.dateparser;

import java.util.Locale;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class FormatParser implements DateParser {

	private DateTimeFormatter formatter;

	public FormatParser(String format, String timezone, String locale) {
		this.formatter = DateTimeFormat.forPattern(format);

		if (timezone != null) {
			this.formatter = this.formatter.withZone(DateTimeZone
					.forID(timezone));
		} else {
			this.formatter = this.formatter.withOffsetParsed();
		}

		if (locale != null) {
			this.formatter = this.formatter.withLocale(Locale
					.forLanguageTag(locale));
		}
	}

	@Override
	public DateTime parse(String input) {
		return this.formatter.parseDateTime(input);
	}

	public static void main(String[] args) {
		LocalDate date = LocalDate.now();
		DateTimeFormatter fmt = DateTimeFormat.forPattern("d MMM, yyyy")
				.withLocale(Locale.ENGLISH);
		String str = date.toString(fmt);
		System.out.println(str);

		long a = DateTimeFormat.forPattern("dd MMM YYYY:HH:mm:ss")
				.withLocale(Locale.ENGLISH).parseMillis("13 May 2015:22:46:59");
		System.out.println(a);

		String input = "2015/05/06 10:31:20.427";
		FormatParser p = new FormatParser("YYYY/MM/dd HH:mm:ss.SSS", null, null);
		System.out.println(p.parse(input));

		input = "13 May 2015:22:46:59";
		p = new FormatParser("dd MMM YYYY:HH:mm:ss", null, "en");
		System.out.println(p.parse(input));
	}
}

@@ -1261,16 +1261,196 @@
 ntln(a);
+%0A%09%09%0A%09%09DateTime b = DateTimeFormat.forPattern(%22dd MMM YYYY:HH:mm:ss%22)%0A%09%09%09%09.withLocale(Locale.forLanguageTag(%22en%22)).parseDateTime(%2213 May 2015:22:46:59%22);%0A%09%09System.out.println(b);%0A%09%09
 %0A%0A%09%09Stri

test locale