java date类的日期时间等操作实例。java date类 常用方法介绍。时间格式化,比较时间日期,String类型转换为Date类型的方法实例等。
java创建一个日期对象实例源码:
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());
} }
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateExample2 {
public static void main(String[] args) {
SimpleDateFormat bartDateFormat = new SimpleDateFormat(“EEEE-MMMM-dd-yyyy”);
Date date = new Date();
System.out.println(bartDateFormat.format(date));
} }
java将文本数据解析成日期对象
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateExample3 {
public static void main(String[] args) {
// Create a date formatter that can parse dates of
// the form MM-dd-yyyy.
SimpleDateFormat bartDateFormat = new SimpleDateFormat(“MM-dd-yyyy”);
// Create a string containing a text date to be parsed.
String dateStringToParse = “9-29-2001″;
try {
// Parse the text version of the date.
// We have to perform the parse method in a
// try-catch construct in case dateStringToParse
// does not contain a date in the format we are expecting.
Date date = bartDateFormat.parse(dateStringToParse);
// Now send the parsed date as a long value
// to the system output.
System.out.println(date.getTime());
}
catch (Exception ex) {
System.out.println(ex.getMessage());
}
} }
java时间比较实例源码:
import java.text.*;
import java.util.*;
public class TimeCompare{
}
在java里怎么实现当前时间格式为yyyy-mm-dd hh:mm:ss以及当前时间后30分钟:
import java.text.*;
import java.util.*;
public class L{
public static void main(String[] args){
Date date = new Date(System.currentTimeMillis());
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM,Locale.CHINA);
String dt = df.format(date);
System.out.println(dt);
date = new Date(System.currentTimeMillis() + 30 * 60 * 1000); //半小时以后的时间
dt = df.format(date);
System.out.println(dt);
}}
—————————
java 测试执程序行时间:
long time1 = System.currentTimeMillis();
stmt.executeQuery(sql);
long time2 = System.currentTimeMillis();
System.out.println(“====SqlServer “+(time2 – time1) + ” ms”);