zoukankan      html  css  js  c++  java
  • java 获取当前日期和特殊日期格式转换

     1.获取当前日期:

    package com.infomorrow.dao;
    
    
    import java.sql.Timestamp;
    import java.util.Calendar;
    import java.util.TimeZone;
    
    import org.junit.Test;
    
    public class test_date {
    @Test
    public void test(){
        Timestamp today = new Timestamp(Calendar.getInstance(TimeZone.getTimeZone("GMT+8")).getTimeInMillis());
        int year=today.getYear()+1900;
        int month=today.getMonth()+1;
        int day = today.getDate();
        String date = year+"-"+month+"-"+day;
        System.out.println("date:"+date);
        System.out.println(today);
        }
    }
    //输出
    date:2013-12-17
    2013-12-17 13:04:30.809

    2.日期格式转换

    package utils;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Locale;
    public class DateParser {
    public static void main(String[] args) {
    //java时间格式转换: Mar 30, 2014 3:49:2 PM 如何转换成标准的java Date对象
    /*
    LetterDate or Time ComponentPresentationExamples
    GEra designatorTextAD
    yYearYear1996; 96
    MMonth in yearMonthJuly; Jul; 07
    wWeek in yearNumber27
    WWeek in monthNumber2
    DDay in yearNumber189
    dDay in monthNumber10
    FDay of week in monthNumber2
    EDay in weekTextTuesday; Tue
    aAm/pm markerTextPM
    HHour in day (0-23)Number0
    kHour in day (1-24)Number24
    KHour in am/pm (0-11)Number0
    hHour in am/pm (1-12)Number12
    mMinute in hourNumber30
    sSecond in minuteNumber55
    SMillisecondNumber978
    zTime zoneGeneral time zonePacific Standard Time; PST; GMT-08:00
    ZTime zoneRFC 822 time zone-0800
    */
    SimpleDateFormat sdf = new SimpleDateFormat("MMM d, yyyy K:m:s a",Locale.ENGLISH);
    Date d2 = null;
    try {
    d2 = sdf.parse("Mar 30, 2014 3:49:2 PM");
    } catch (ParseException e) {
    e.printStackTrace();
    }
    Date d3 = new Date();
    System.out.println("d2 ====== "+d2);
    System.out.println("d3 ====== "+sdf.format(d3));
    }
    }
  • 相关阅读:
    在应用程序中利用Jena API处理OWL本体
    Encoded vs Literal, RPC vs Document
    DWR、XMLHTTP、XMLRPC和Flex
    北京的第一场雪
    让IE浏览器提示下载或直接打开word文档
    色拉英语第一集第一幕:记得说“请”
    色拉英语第一集第三幕:凯文在家吗?
    30天敏捷结果(30):提升敏捷结果
    生活:兔年春节家庭寻宝习俗
    敏捷个人:2011/1/26聊天记录(沟通、优势)
  • 原文地址:https://www.cnblogs.com/amosli/p/3478236.html
Copyright © 2011-2022 走看看