A. 用java怎麼使變數每秒鍾加1急!!
其實就是讓線程休眠一秒就可以了很簡單
例子如下
package com.zhh.test;
public class Count {
public static void main(String[] args){
int count = 0;
while(true){
System.out.println(count++);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
B. 如何實現對一個日期進行加1
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;public class DateByte { public static void main(String[] args) throws ParseException, IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
/*
* 將yyyy-1-1轉換成yyyy-01-01
*/
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(str);
String str2 = sdf.format(date);
String year = str2.substring(0, 4);
String month = str2.substring(5, 7);
String day = str2.substring(8, 10);
Calendar c = Calendar.getInstance();
c.set(Integer.parseInt(year), Integer.parseInt(month), Integer
.parseInt(day)); c.add(Calendar.DAY_OF_MONTH, 1);
year = Integer.toString(c.get(Calendar.YEAR));
month = Integer.toString(c.get(Calendar.MONTH));
day = Integer.toString(c.get(Calendar.DATE));
String newDate = year + "-" + month + "-" + day;
System.out.println(newDate);
}
}
C. java 取系統時間並加1秒鍾
packagetest;
importjava.util.Calendar;
publicclassJButtonTest
{
publicstaticvoidmain(String[]args)
{
Calendarcalendar=Calendar.getInstance();
System.out.println(calendar.getTime());
calendar.add(Calendar.SECOND,1);
System.out.println(calendar.getTime());
}
}
D. java 給固定日期(字元串)加上當前的 時分秒
java.text.SimpleDateFormat simpleDateFormat = new java.text.SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
java.util.Date currentTime = new java.util.Date();
String time = simpleDateFormat.format(currentTime).toString();
當前系統時間:time
E. 在此java程序上,添加實現一個方法add(),增加一秒後,並顯示時間。
class Clock{
int hour,min,sec;
Clock(int h,int m,int s){
hour=h;
min=m;
sec=s;
}
Clock(){}
void add(){
sec++;
}
void show()
{
System.out.printf("時間 %d:%d:%d",hour,min,sec);
System.out.println();
}
}
public class TextClass{
public static void main(String args[]){
Clock c1=new Clock(4,20,9);
Clock c2=new Clock(2,3,4);
Clock c3=new Clock(14,2,9);
c1.show();
c2.show();
c3.show();
c3.add();
c3.show();
}
}
F. java 給固定日期(字元串)加上時分秒
如果你用的是 Java8:
importjava.time.Duration;
importjava.time.LocalTime;
publicclassTest{
=LocalTime.of(0,0,0);
publicstaticvoidmain(String[]args)throwsException{
LocalTimetime=LocalTime.parse("21:53:00");
LocalTimeaugment=LocalTime.parse("01:50:22");
LocalTimetime2=plusTime(time,augment);
System.out.println("time2:"+time2);
}
/**
*在current的基礎上增加augment所表示的時間(間隔)
*/
(LocalTimecurrent,LocalTimeaugment){
Durationration=Duration.between(START,augment);
returncurrent.plus(ration);
}
}
運行:
G. java如何進行時間加減
1.用java.util.Calender來實現
Calendar calendar=Calendar.getInstance();
calendar.setTime(new Date());
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//今天的日期
calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)+1);//讓日期加1
System.out.println(calendar.get(Calendar.DATE));//加1之後的日期Top
2.用java.text.SimpleDateFormat和java.util.Date來實現
Date d=new Date();
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
System.out.println("今天的日期:"+df.format(d));
System.out.println("兩天前的日期:" + df.format(new Date(d.getTime() - 2 * 24 * 60 * 60 * 1000)));
System.out.println("三天後的日期:" + df.format(new Date(d.getTime() + 3 * 24 * 60 * 60 * 1000)));
GregorianCalendar gc=new GregorianCalendar();
gc.setTime(new Date);
gc.add(field,value);
value為正則往後,為負則往前
field取1加1年,取2加半年,取3加一季度,取4加一周
取5加一天....
H. java 指定一個起始時間,用循環列印,每列印九個就加一秒,繼續列印。求大神幫忙
你那個太亂了,稍微幫你整理了下
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringtoDate {
public static void main(String args[]) throws Exception {
String input = "[10/11/2015:00:00:01 +0800]";
SimpleDateFormat format = new SimpleDateFormat("[dd/MM/yyyy:HH:mm:ss Z]");
Date d = new Date();
d = format.parse(input);
System.out.println("起始時間:" + format.format(d));
while (true) {
Thread.sleep(1000);
String dateString = format.format(new Date(d.getTime() + 1000));
d = format.parse(dateString);
for (int i = 0; i < 9; i++) {
Thread.sleep(500);
System.out.println(dateString);
}
}
}
}