import java.util.Date;public class DateExample1 { public static void main(String[] args) { // Get the system date/time Date date = new Date(); System.out.println(date.getTime()); } }
Date now = new Date();如果要一些复杂操作的话就需GregorianCalendar类,看看文档,那里面有许多方法,估计够用
import java.util.GregorianCalendar; import java.util.Date; import java.text.DateFormat;public class DateExample5 { public static void main(String[] args) { DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL); // Create our Gregorian Calendar. GregorianCalendar cal = new GregorianCalendar(); // Set the date and time of our calendar // to the system's date and time cal.setTime(new Date()); System.out.println("System Date: " + dateFormat.format(cal.getTime())); // Set the day of week to FRIDAY cal.set(GregorianCalendar.DAY_OF_WEEK, GregorianCalendar.FRIDAY); System.out.println("After Setting Day of Week to Friday: " + dateFormat.format(cal.getTime())); int friday13Counter = 0; while (friday13Counter <= 10) { // Go to the next Friday by adding 7 days. cal.add(GregorianCalendar.DAY_OF_MONTH, 7); // If the day of month is 13 we have // another Friday the 13th. if (cal.get(GregorianCalendar.DAY_OF_MONTH) == 13) { friday13Counter++; System.out.println(dateFormat.format(cal.getTime())); } } } }
这个也不错!import java.text.DateFormat; import java.util.Date;public class DateExample4 { public static void main(String[] args) { Date date = new Date(); DateFormat shortDateFormat = DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.SHORT); DateFormat mediumDateFormat = DateFormat.getDateTimeInstance( DateFormat.MEDIUM, DateFormat.MEDIUM); DateFormat longDateFormat = DateFormat.getDateTimeInstance( DateFormat.LONG, DateFormat.LONG); DateFormat fullDateFormat = DateFormat.getDateTimeInstance( DateFormat.FULL, DateFormat.FULL); System.out.println(shortDateFormat.format(date)); System.out.println(mediumDateFormat.format(date)); System.out.println(longDateFormat.format(date)); System.out.println(fullDateFormat.format(date)); } }
完整例子import java.util.*; import java.text.*; public class test { public static void main(String[] args) { Date aDate = new Date();//取得系统时间 DateFormat fmt = DateFormat.getDateTimeInstance(); String dateString = fmt.format(aDate);//用DateFormat对象fmt格式化Date对象aDate并赋值给String变量dateString System.out.println(dateString); } }
public static void main(String[] args) {
// Get the system date/time
Date date = new Date(); System.out.println(date.getTime());
}
}
关注中……
hehe,借用你的分
这是一个long型数据,指的从1970年元旦到现在地毫秒数。建议你看看GregorianCalendar类。
http://www.zdnet.com.cn/developer/code/story/0,2000081534,20030446-2,00.htm
import java.util.Date;
import java.text.DateFormat;public class DateExample5 { public static void main(String[] args) {
DateFormat dateFormat =
DateFormat.getDateInstance(DateFormat.FULL); // Create our Gregorian Calendar.
GregorianCalendar cal = new GregorianCalendar(); // Set the date and time of our calendar
// to the system's date and time
cal.setTime(new Date()); System.out.println("System Date: " +
dateFormat.format(cal.getTime())); // Set the day of week to FRIDAY
cal.set(GregorianCalendar.DAY_OF_WEEK,
GregorianCalendar.FRIDAY);
System.out.println("After Setting Day of Week to Friday: " +
dateFormat.format(cal.getTime())); int friday13Counter = 0;
while (friday13Counter <= 10) { // Go to the next Friday by adding 7 days.
cal.add(GregorianCalendar.DAY_OF_MONTH, 7); // If the day of month is 13 we have
// another Friday the 13th.
if (cal.get(GregorianCalendar.DAY_OF_MONTH) == 13) {
friday13Counter++;
System.out.println(dateFormat.format(cal.getTime()));
}
}
}
}
import java.util.Date;public class DateExample4 { public static void main(String[] args) {
Date date = new Date(); DateFormat shortDateFormat =
DateFormat.getDateTimeInstance(
DateFormat.SHORT,
DateFormat.SHORT); DateFormat mediumDateFormat =
DateFormat.getDateTimeInstance(
DateFormat.MEDIUM,
DateFormat.MEDIUM); DateFormat longDateFormat =
DateFormat.getDateTimeInstance(
DateFormat.LONG,
DateFormat.LONG); DateFormat fullDateFormat =
DateFormat.getDateTimeInstance(
DateFormat.FULL,
DateFormat.FULL); System.out.println(shortDateFormat.format(date));
System.out.println(mediumDateFormat.format(date));
System.out.println(longDateFormat.format(date));
System.out.println(fullDateFormat.format(date));
}
}
import java.text.*;
public class test
{
public static void main(String[] args)
{
Date aDate = new Date();//取得系统时间
DateFormat fmt = DateFormat.getDateTimeInstance();
String dateString = fmt.format(aDate);//用DateFormat对象fmt格式化Date对象aDate并赋值给String变量dateString
System.out.println(dateString);
}
}