1 package org.andromda.utils;
2
3 import org.apache.commons.lang.time.DateUtils;
4
5
6
7
8 public class DateUtilsHelper
9 extends DateUtils
10 {
11
12 private static final FormatPattern[] JAVA2PERL_FORMAT_PATTERNS = {new FormatPattern("y{4,}", "%Y"),
13 new FormatPattern("y{1,3}", "%y"),
14 new FormatPattern("M{4,}", "%B"),
15 new FormatPattern("M{3}", "%b"),
16 new FormatPattern("M{1,2}", "%m"),
17 new FormatPattern("d{4,}", "%A"),
18 new FormatPattern("d{3,}", "%a"),
19 new FormatPattern("d{2}", "%d"),
20 new FormatPattern("d{1}", "%e"),
21 new FormatPattern("E{4,}", "%A"),
22 new FormatPattern("E{1,3}", "%a"),
23 new FormatPattern("H{2,}", "%H"),
24 new FormatPattern("H{1}", "%k"),
25 new FormatPattern("h{2,}", "%I"),
26 new FormatPattern("h{1}", "%l"),
27 new FormatPattern("D{3}", "%j"),
28 new FormatPattern("s{2,}", "%S"),
29 new FormatPattern("s{1}", "%s"),
30 new FormatPattern("m{2,}", "%M")};
31
32
33
34
35
36
37 public static String formatJavaToPerl(String javaFormat)
38 {
39 String perlFormat = null;
40
41 if (javaFormat != null)
42 {
43 perlFormat = javaFormat;
44
45
46
47 for (int i = 0; i < JAVA2PERL_FORMAT_PATTERNS.length; i++)
48 {
49 final FormatPattern formatPattern = JAVA2PERL_FORMAT_PATTERNS[i];
50 perlFormat = perlFormat.replaceAll(formatPattern.getPattern(), "%%%" + i + "%%%");
51 }
52
53 for (int i = 0; i < JAVA2PERL_FORMAT_PATTERNS.length; i++)
54 {
55 final FormatPattern formatPattern = JAVA2PERL_FORMAT_PATTERNS[i];
56 perlFormat = perlFormat.replaceAll("%%%" + i + "%%%", formatPattern.getReplacement());
57 }
58 }
59
60 return perlFormat;
61 }
62
63 private static final String[] PERL_TIME_FORMATS = new String[]{"%H", "%I", "%k", "%l", "%p", "%P", "%s", "%S"};
64
65
66
67
68
69
70 public static boolean containsTimeFormat(String perlFormat)
71 {
72 boolean containsTimeFormat = false;
73
74 for (int i = 0; i < PERL_TIME_FORMATS.length && !containsTimeFormat; i++)
75 {
76 String timeFormatPattern = PERL_TIME_FORMATS[i];
77 containsTimeFormat = perlFormat.contains(timeFormatPattern);
78 }
79
80 return containsTimeFormat;
81 }
82
83 private static final class FormatPattern
84 {
85 private final String pattern;
86 private final String replacement;
87
88 public FormatPattern(
89 final String formatPattern,
90 final String replacement)
91 {
92 this.pattern = formatPattern;
93 this.replacement = replacement;
94 }
95
96 public String getPattern()
97 {
98 return pattern;
99 }
100
101 public String getReplacement()
102 {
103 return replacement;
104 }
105 }
106 }