A. java方法的运行时间
publicstaticvoidmain(String[]args){
longstart=System.currentTimeMillis();
A();
System.out.println(System.currentTimeMillis()-start);
}
B. java中处理时间间隔
import java.awt.FlowLayout;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Clock extends JFrame {
private static final long serialVersionUID = 4168118275271453942L;
private JLabel jlabel = null;
public Clock() {
super("时钟");
init();
}
private void init() {
jlabel = new JLabel();
this.setLayout(new FlowLayout(FlowLayout.CENTER));
this.add(jlabel);
this.setLocation(450, 450);
this.setSize(120, 60);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
TimeThread time = new TimeThread(jlabel);
Thread thread = new Thread(time);
thread.start();
}
class TimeThread implements Runnable{
private JLabel label = null;
public TimeThread(JLabel label ){
this.label = label;
}
public void run() {
while(true){
try {
Thread.sleep(1000);
showTime();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void showTime() {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
label.setText(sdf.format(date));
}
}
public static void main(String[] args) {
new Clock();
}
}
专门给你写的!很简单看看吧
C. java 日期处理
Date now = new Date(System.currentTimeMillis());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//定义日期格式
String nowString = sdf.format(now);//生成格式为yyyy-MM-dd HH:mm:ss的日期字符串对象
Date formatedNow = sdf.parse(nowString);//生成格式为yyyy-MM-dd HH:mm:ss的日期对象
D. java 日期格式的处理
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
String text = sdf.format(new Date());
System.out.println(text);
}
}
输出:
2012-05-12 13:30:33:963
================================================
字母 日期或时间元素 表示 示例
G Era 标志符 Text AD
y 年 Year 1996; 96
M 年中的月份 Month July; Jul; 07
w 年中的周数 Number 27
W 月份中的周数 Number 2
D 年中的天数 Number 189
d 月份中的天数 Number 10
F 月份中的星期 Number 2
E 星期中的天数 Text Tuesday; Tue
a Am/pm 标记 Text PM
H 一天中的小时数(0-23) Number 0
k 一天中的小时数(1-24) Number 24
K am/pm 中的小时数(0-11) Number 0
h am/pm 中的小时数(1-12) Number 12
m 小时中的分钟数 Number 30
s 分钟中的秒数 Number 55
S 毫秒数 Number 978
z 时区 General time zone Pacific Standard Time; PST; GMT-08:00
Z 时区 RFC 822 time zone -0800
具体可查看JDK API SimpleDateFormat类
E. 关于java中的时间处理问题!
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class TestDate {
/**
*
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
printTuesday("2010-4-13","2010-5-13");
}
public static void printTuesday(String begin,String end){
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date begin_d;
Date end_d;
Calendar begin_c = Calendar.getInstance();
Calendar end_c = Calendar.getInstance();
try {
begin_d = df.parse(begin);
end_d = df.parse(end);
begin_c.setTime(begin_d);
end_c.setTime(end_d);
end_c.add(Calendar.DAY_OF_YEAR, 1); //结束日期下滚一下,为了包含最后一天
while(begin_c.before(end_c)){
if(begin_c.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY){
System.out.println(new java.sql.Date(begin_c.getTimeInMillis()));
}
begin_c.add(Calendar.DAY_OF_YEAR, 1);
}
} catch (ParseException e) {
System.out.println("日期格式不对,必须为:yyyy-MM-dd,例:2010-04-13");
return;
}
}
}
F. JAVA如何正确地处理时间/时区
使用java.time及子包的相关类,网上可以搜用法。
G. java中如何对日期进行格式处理
import java.text.SimpleDateFormat;
import java.util.Date;
public class test {
public static void main(String []aa){
SimpleDateFormat dateformat1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");
String a1=dateformat1.format(new Date());
System.out.println("时间2:"+a1);
System.out.println(new Date().getYear()+1900);
SimpleDateFormat dateformat2=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E ");
String a2=dateformat2.format(new Date());
System.out.println("时间2:"+a2);
}
}
执行结果:
时间2:2012-5-12 14:27:59 星期六
2006
时间2:2012年5月12日 13时27分59秒 星期六
就是SimpleDateFormate这个对象
再调用它的formate()方法
H. java处理日期数据
我的建议是,数据库中不要用日期类型,用字符型。
java将日期转化成指定的格式的字符串后,再保存到数据库中。
这样,不依赖数据库,也不依赖操作系统。(不同的数据库,不同的操作系统日期表示可能不同。)
I. java中常用的时间和时间戳的处理
手打,有问题再问
longcurrentTimeMillis=System.currentTimeMillis();//时间戳,毫秒数
SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-dd");//时间格式化类
Datedate=sdf.parse("2012-01-01");//解析到一个时间
StringdateStr=sdf.format(newDate());//将时间格式化为一定格式