forked from apache/struts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WW-5016 Introduces different format adapters to allow use different APIs
- Loading branch information
1 parent
804e154
commit e3dff76
Showing
10 changed files
with
280 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
core/src/main/java/org/apache/struts2/components/date/DateFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* 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.struts2.components.date; | ||
|
||
import java.time.temporal.TemporalAccessor; | ||
|
||
/** | ||
* Allows defines a wrapper around different formatting APIs, like old SimpleDateFormat | ||
* and new DateTimeFormatter introduced in Java 8 Date/Time API | ||
* | ||
* New instance will be injected using {@link org.apache.struts2.StrutsConstants#STRUTS_DATE_FORMATTER} | ||
*/ | ||
public interface DateFormatter { | ||
|
||
String format(TemporalAccessor temporal, String format, String defaultFormat); | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
core/src/main/java/org/apache/struts2/components/date/DateTimeFormatterAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* 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.struts2.components.date; | ||
|
||
import com.opensymphony.xwork2.ActionContext; | ||
|
||
import java.time.format.DateTimeFormatter; | ||
import java.time.format.FormatStyle; | ||
import java.time.temporal.TemporalAccessor; | ||
import java.util.Locale; | ||
|
||
public class DateTimeFormatterAdapter implements DateFormatter { | ||
|
||
@Override | ||
public String format(TemporalAccessor temporal, String format, String defaultFormat) { | ||
DateTimeFormatter dtf; | ||
Locale locale = ActionContext.getContext().getLocale(); | ||
if (format == null) { | ||
if (defaultFormat != null) { | ||
dtf = DateTimeFormatter.ofPattern(defaultFormat, locale); | ||
} else { | ||
dtf = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM) | ||
.withLocale(locale); | ||
} | ||
} else { | ||
dtf = DateTimeFormatter.ofPattern(format, locale); | ||
} | ||
return dtf.format(temporal); | ||
} | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
core/src/main/java/org/apache/struts2/components/date/SimpleDateFormatAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* 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.struts2.components.date; | ||
|
||
import com.opensymphony.xwork2.ActionContext; | ||
|
||
import java.text.DateFormat; | ||
import java.text.SimpleDateFormat; | ||
import java.time.Instant; | ||
import java.time.temporal.TemporalAccessor; | ||
import java.util.Date; | ||
import java.util.Locale; | ||
|
||
public class SimpleDateFormatAdapter implements DateFormatter { | ||
|
||
@Override | ||
public String format(TemporalAccessor temporal, String format, String defaultFormat) { | ||
DateFormat df; | ||
Locale locale = ActionContext.getContext().getLocale(); | ||
if (format == null) { | ||
if (defaultFormat != null) { | ||
df = new SimpleDateFormat(defaultFormat, locale); | ||
} else { | ||
df = SimpleDateFormat.getDateInstance(DateFormat.MEDIUM, locale); | ||
} | ||
} else { | ||
df = new SimpleDateFormat(format, locale); | ||
} | ||
return df.format(new Date(Instant.from(temporal).toEpochMilli())); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.