Inadvertently found that for the time string to date class, do not have to write their own conversion class, Spring MVC has implemented this feature, or annotation-based, easy and convenient, using Org.springframework.format.support.FormattingConversionServiceFactoryBeanAfter that, just add a note to VO.To introduce a class library when using Joda-time-n.n.jarJava code
@DateTimeFormat (pattern="Yyyy-mm-dd")
Private Date Daterangestart; //
; Bean> List> Property> Bean>The initialization binder method is then created in a controller that requires a type conversion, and the received string is converted to a date with the corresponding editor, with the following code:@Controller Public class Myformcontroller { @InitBinder protectedvoid Initbinder (webdatabinder Binder) { new SimpleDateFormat ("Yyyy-mm-dd"); Dateformat.setlenient (false); Binder.registercustomeditor
When using spring's @datetimeformat to format the date type, an error occurs:Org.springframework.validation.bindexception:org.springframework.validation.beanpropertybindingresult:1 errorsField error in object ' Modelinfoexample ' on field ' birthday ':Rejected value [2014-12-09];codes [Typemismatch.modelinfoexample.birthday,typemismatch.birthday,typemismatch.java.util.date,typemismatch];arguments [org.springframework.context.support.DefaultMessageSour
@JsonInclude (value=include.non_null): The function that is used on the head of the method class of the entity class is that the parameter of the entity class is queried to a NULL that is not displayed@DateTimeFormat: The time value auto-transform used to receive the front-end pass can be date can be a string note format as Yyyy-mm-dd yyyy/mm/ddd@JsonFormat用于输出的时候时间格式解析 Use of annotations @JsonInclude, @JsonFormat, @
controller method get HttpServletRequest request = (servletrequestattributes) Requestcontextholder.getrequestattributes ()). Getrequest (); Mode 4: Add the following method to controller, which executes before executing this controller method
@ModelAttribute
private void Initservlet (HttpServletRequest request,httpservletresponse response) {
// String p=request.getparameter ("P");
this.req=request;//instance variables, thread security issues, can be saved using threadlocal mode
}
("{0: g}", 2.5); 2.5N or N digital console. Write ("{0: n}", 2500000); 2,500,000.00X or X hexadecimal console. Write ("{0: x}", 250 );Console. Write ("{0: x}", 0 xFFFF );FaFFFF
Ii. Date formatting
String in standard datetime formatIf the format strings only contain a single format specifier listed in the following table, they are interpreted as standard format specifiers. If the specified format character is a single character and does not contain the following table, an exception is thrown.
with other custom format specifiers or "%" characters, it is interpreted as a custom format specifier.Dd displays the current date of the month, represented by a number between 1 and 31, including 1 and 31. If the date has only one digit (1-9), format it with a leading 0 (01-09 ).Ddd displays the abbreviation of the specified datetime date. If no specific valid format is provided Program (Implement a non-empty object of iformatprovider with the expected attributes), use the abbreviateddaynames
Dim dT as dateConsole. writeline (datevalue ("H20/10/12"). Date. tow.datestring)Datetime. tryparseexact ("20081010", "yyyymmdd", nothing, globalization. datetimestyles. None, DT)
Dim CI as cultureinfo = new cultureinfo ("Ja-JP ")Dim Cal as calendar = new japanesecalendarCI. datetimeformat. Calendar = new japanesecalendar
Dim strwri as string = CI. datetimeformat. getabbreviatederaname (calendar. currena
cultureinfo class (under the system. Globalization namespace) is used to control the region where the date is displayed.
The japanesecalendar class (under the namespace system. Globalization) indicates the Japanese calendar.
The following code converts dates:
1) 2006/09/26 to 18/09/26
Cultureinfo CI = new cultureinfo ("Ja-JP ");
Calendar Cal = new japanesecalendar ();
CI. datetimeformat. Calendar = Cal;
Datetime dt = datetime. parse ("2006/09/26 ");
may not be accurate because Saturday and Sunday can generate "s"). "2-digit" and "numeric" are mapped to 2-digit strings or full length numeric strings, such as "70" and "1970".
Most of the options that are ultimately used are the options that are requested. However, if you do not specify the weekday/year/month/day/hour/minute/second of the request, then Year/month/day will be added to the option you provide.
In addition, there are some special options:
Hour12
Specifies whether th
Formatting is primarily about letting data enter in a particular format, getting the results of a particular style (which can also be attributed to data type conversions, mostly to other types of string types).Here is an example of @numberformat and @DateTimeFormat notes:First, for the problem:When you enter a date, it may be written as the format type of 1990-11-23, which is a string type, and the background is not recognized by the date type, and it
Date type data sent over the front end cannot receive
And then I found a single date matching method.
@DateTimeFormat (pattern= "Yyyy-mm-dd HH:mm:ss")It's okay.
@JsonFormat (pattern= "Yyyy-mm-dd HH:mm:ss", timezone = "gmt+8")
Returns the time of the front-end string format
Reference
When querying the database, the date type returned is Java.sql.date, The browser is set to Java.util.date in a custom node object, but the sql.date loses in
Standard | string standard DateTime format string contains one of the format specifier characters in the following table. If the format specifier is not in the following table, a Run-time exception is thrown. If the format string is longer than a single character (even if the extra characters are blank), the format string is interpreted as a custom format string.
Note that the output string produced by these format specifiers is affected by the settings in the Regional Options control Panel. Th
org.springframework.format.datetime;public final class DateFormatter implements Formatter
The Spring team welcomes contributions from the Community to Formatter. You can contribute through http://jira.springframework.org.
Annotation-driven formatting
As you will see later, field formatting can be configured by field type or annotation. AnnotationFormatterFactory (Note: Associate A with the Printer and Parser generated by the Factory by.):
package org.springframework.format;public interface An
Executeresult method, Using Json.NET to complete the serialization work, the code for the JsonResultPro.cs file is as follows:
namespace jsondatemvc.common{using System; using system.web; using SYSTEM.WEB.MVC; using Newtonsoft.json; using Newtonso Ft. Json.converters; public class Jsonresultpro:jsonresult {public Jsonresultpro () {} public Jsonresultpro (object data, Jsonrequestbehavior b Ehavior) {base. data = data; Base. Jsonrequestbehavior = behavior; This.
also be provided:Support for type conversion of form parameters using Conversionservice instancesSupport for formatting data types using @NumberFormat annotation, @DateTimeFormat annotationsSupport for JSR 303 validation of JavaBean instances using @Valid annotationsSupport for using @RequestBody and @ResponseBody annotations@InitBinderThe Webdatabinder object can be initialized by a method identified by the @InitBinder. Webdatabinder is a subclass o
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.