Java日期處理工具類

openkk 12年前發布 | 25K 次閱讀 Java Java開發

/*

  • 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.ftpserver.util;

import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.TimeZone;

/**

    // year
    sb.append(cal.get(Calendar.YEAR));

    // month
    int month = cal.get(Calendar.MONTH) + 1;
    if (month < 10) {
        sb.append('0');
    }
    sb.append(month);

    // date
    int date = cal.get(Calendar.DATE);
    if (date < 10) {
        sb.append('0');
    }
    sb.append(date);

    // hour
    int hour = cal.get(Calendar.HOUR_OF_DAY);
    if (hour < 10) {
        sb.append('0');
    }
    sb.append(hour);

    // minute
    int min = cal.get(Calendar.MINUTE);
    if (min < 10) {
        sb.append('0');
    }
    sb.append(min);

    // second
    int sec = cal.get(Calendar.SECOND);
    if (sec < 10) {
        sb.append('0');
    }
    sb.append(sec);

    // millisecond
    sb.append('.');
    int milli = cal.get(Calendar.MILLISECOND);
    if (milli < 100) {
        sb.append('0');
    }
    if (milli < 10) {
        sb.append('0');
    }
    sb.append(milli);
    return sb.toString();
}
/*
 *  Parses a date in the format used by the FTP commands 
 *  involving dates(MFMT, MDTM)
 */
public final static Date parseFTPDate(String dateStr) throws ParseException{
    return FTP_DATE_FORMAT.get().parse(dateStr);

}

}</pre>

 本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!