当前位置: 技术问答>java相关
Java中的时间问题
来源: 互联网 发布时间:2015-03-14
本文导语: 请问Java中有没有方法可以得到2位月份,例如1月得到的是01 得到年份呢? 谢谢! | Get the current Date and Time import java.util.*; public class Now { public static void main(String arg[]) { /* ...
请问Java中有没有方法可以得到2位月份,例如1月得到的是01
得到年份呢?
谢谢!
得到年份呢?
谢谢!
|
Get the current Date and Time
import java.util.*;
public class Now {
public static void main(String arg[]) {
/*
** on some JDK, the default TimeZone is wrong
** we must set the TimeZone manually!!!
** Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("EST"));
*/
Calendar cal = Calendar.getInstance(TimeZone.getDefault());
String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
java.text.SimpleDateFormat sdf =
new java.text.SimpleDateFormat(DATE_FORMAT);
/*
** on some JDK, the default TimeZone is wrong
** we must set the TimeZone manually!!!
** sdf.setTimeZone(TimeZone.getTimeZone("EST"));
*/
sdf.setTimeZone(TimeZone.getDefault());
System.out.println("Now : " + sdf.format(cal.getTime()));
}
}
Here some formatting possibilities available through the SimpleDateFormat class.
Thanks to T. Guirado for the tip. import java.util.*;
import java.text.*;
public class ShowToday {
public static void main(String args[]) {
ShowToday st = new ShowToday();
st.demo();
}
public void demo() {
System.out.println(easyDateFormat("dd MMMMM yyyy"));
System.out.println(easyDateFormat("yyyyMMdd"));
System.out.println(easyDateFormat("dd.MM.yy"));
System.out.println(easyDateFormat("MM/dd/yy"));
System.out.println(easyDateFormat("yyyy.MM.dd G 'at' hh:mm:ss z"));
System.out.println(easyDateFormat("EEE, MMM d, ''yy"));
System.out.println(easyDateFormat("h:mm a"));
System.out.println(easyDateFormat("H:mm:ss:SSS"));
System.out.println(easyDateFormat("K:mm a,z"));
System.out.println(easyDateFormat("yyyy.MMMMM.dd GGG hh:mm aaa"));
}
public String easyDateFormat (String format) {
Date today = new Date();
SimpleDateFormat formatter = new SimpleDateFormat(format);
String datenewformat = formatter.format(today);
return datenewformat;
}
}
import java.util.*;
public class Now {
public static void main(String arg[]) {
/*
** on some JDK, the default TimeZone is wrong
** we must set the TimeZone manually!!!
** Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("EST"));
*/
Calendar cal = Calendar.getInstance(TimeZone.getDefault());
String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
java.text.SimpleDateFormat sdf =
new java.text.SimpleDateFormat(DATE_FORMAT);
/*
** on some JDK, the default TimeZone is wrong
** we must set the TimeZone manually!!!
** sdf.setTimeZone(TimeZone.getTimeZone("EST"));
*/
sdf.setTimeZone(TimeZone.getDefault());
System.out.println("Now : " + sdf.format(cal.getTime()));
}
}
Here some formatting possibilities available through the SimpleDateFormat class.
Thanks to T. Guirado for the tip. import java.util.*;
import java.text.*;
public class ShowToday {
public static void main(String args[]) {
ShowToday st = new ShowToday();
st.demo();
}
public void demo() {
System.out.println(easyDateFormat("dd MMMMM yyyy"));
System.out.println(easyDateFormat("yyyyMMdd"));
System.out.println(easyDateFormat("dd.MM.yy"));
System.out.println(easyDateFormat("MM/dd/yy"));
System.out.println(easyDateFormat("yyyy.MM.dd G 'at' hh:mm:ss z"));
System.out.println(easyDateFormat("EEE, MMM d, ''yy"));
System.out.println(easyDateFormat("h:mm a"));
System.out.println(easyDateFormat("H:mm:ss:SSS"));
System.out.println(easyDateFormat("K:mm a,z"));
System.out.println(easyDateFormat("yyyy.MMMMM.dd GGG hh:mm aaa"));
}
public String easyDateFormat (String format) {
Date today = new Date();
SimpleDateFormat formatter = new SimpleDateFormat(format);
String datenewformat = formatter.format(today);
return datenewformat;
}
}
|
import java.util.*;
import java.text.*;
......
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd");
java.util.Date currentTime_1 = new java.util.Date();
String dateString=formatter.format(currentTime_1);
dateString.substring(5,7);
.....
import java.text.*;
......
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd");
java.util.Date currentTime_1 = new java.util.Date();
String dateString=formatter.format(currentTime_1);
dateString.substring(5,7);
.....