将HH:mm:ss格式转换为java.util.Date格式?

时间:2015-10-03 10:28:33

标签: jquery hibernate spring-mvc datetime

我正在做一个基于spring,hibernate的应用程序。我需要一个要求,即我有一个字段只接受时间(不是日期)HH:mm:ss,我希望这个字段必须绑定到dto。但我得到了以下例外:

Field error in object 'reservation' on field 'arrivaltime': rejected value [21:00:3]; codes  
[typeMismatch.reservation.arrivaltime,typeMismatch.arrivaltime,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [reservation.arrivaltime,arrivaltime]; arguments []; default message [arrivaltime]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'arrivaltime'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'arrivaltime': no matching editors or conversion strategy found]] with root cause
org.springframework.validation.BindException:org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'reservation' on field 'arrivaltime': rejected value [21:00:3]; codes [typeMismatch.reservation.arrivaltime,typeMismatch.arrivaltime,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [reservation.arrivaltime,arrivaltime]; arguments []; default message [arrivaltime]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'arrivaltime'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'arrivaltime': no matching editors or conversion strategy found]  

我正在通过JQuery插件选择时间。

jq('#arrivaltime').datetimepicker({
        datepicker:false,
        format:'H:i:j',
        step:60
    });  

我在DTO课堂上的字段是这样的:

@Column(name = "booking_date")
@Temporal(TemporalType.TIME)
private Date booking_date;  

我只想要Time格式来绑定该字段。请帮帮我

1 个答案:

答案 0 :(得分:1)

您可以使用@DateTimeFormat及其pattern属性向框架提示预期的日期模式。以下应该有效

Sub OpenExcelFile()

    Dim oExcel As Excel.Application
    Dim oWB As Excel.Workbook
    Dim xlSheet As Excel.Worksheet
    Dim oneRange As Excel.Range
    Dim aCell As Excel.Range
    Dim intChoice As Integer
    Dim strPath As String
    Dim uiSheet As String

    Set oExcel = New Excel.Application

    'Select the start folder
    Application.FileDialog(msoFileDialogOpen _
    ).InitialFileName = ActiveDocument.path
    'Remove all other filters
    Call Application.FileDialog(msoFileDialogOpen).Filters.Clear
    'Add a custom filter
    Call Application.FileDialog(msoFileDialogOpen).Filters.Add( _
    "Only Excel File Allowed", "*.xl*")
    'only allow the user to select one file
    Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
    'make the file dialog visible to the user
    intChoice = Application.FileDialog(msoFileDialogOpen).Show
    'determine what choice the user made
    If intChoice <> 0 Then
        'get the file path selected by the user
        strPath = Application.FileDialog( _
            msoFileDialogOpen).SelectedItems(1)
    End If
    'open excel file and select sheet
    Set oWB = oExcel.Workbooks.Open(strPath)
    Dim strBuild As String
    'set Array for user input control
    Dim myArray() As Variant
    ReDim myArray(1 To oWB.Sheets.Count)

    'populate input box and array
    For Each xlSheet In oWB.Worksheets
        strBuild = strBuild & xlSheet.Name & vbCrLf
        For i = 1 To oWB.Sheets.Count
            myArray(i) = oWB.Sheets(i).Name
        Next i
    Next xlSheet

    'show inputbox with list of sheets
    strBuild = Left$(strBuild, Len(strBuild) - 2)

    uiSheet = InputBox("Provide a sheet name." & vbNewLine & strBuild)

    'check if User input match with sheet name
    If IsInArray(uiSheet, myArray) Then
        'show excel window
        oExcel.Visible = True

        'sort selected sheet by first column range
        oExcel.Worksheets(uiSheet).Activate
        Set oneRange = oExcel.Range("A1:A150")
        Set aCell = oExcel.Range("A1")
        oneRange.Sort Key1:=aCell, Order1:=xlAscending, Header:=xlYes
    Else
        MsgBox "Please enter a valid name!", vbCritical
    End If

End Sub