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));
}
}
上一篇:Winform 中DataGridView的checkbox列,当修改checkbox状态时实时获得其状态值


下一篇:java 关于hashcode和equals的测试