① 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);
}
}
运行:
② JAVA 时间相加
如果要取出
int getDate()
已过时。 从 JDK 1.1 开始,由 Calendar.get(Calendar.DAY_OF_MONTH) 取代。
int getDay()
已过时。 从 JDK 1.1 开始,由 Calendar.get(Calendar.DAY_OF_WEEK) 取代。
int getHours()
已过时。 从 JDK 1.1 开始,由 Calendar.get(Calendar.HOUR_OF_DAY) 取代。
int getMinutes()
已过时。 从 JDK 1.1 开始,由 Calendar.get(Calendar.MINUTE) 取代。
int getMonth()
已过时。 从 JDK 1.1 开始,由 Calendar.get(Calendar.MONTH) 取代。
int getSeconds()
已过时。 从 JDK 1.1 开始,由 Calendar.get(Calendar.SECOND) 取代。
long getTime()
返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
如果要相加
void setDate(int date)
已过时。 从 JDK 1.1 开始,由 Calendar.set(Calendar.DAY_OF_MONTH, int date) 取代。
void setHours(int hours)
已过时。 从 JDK 1.1 开始,由 Calendar.set(Calendar.HOUR_OF_DAY, int hours) 取代。
void setMinutes(int minutes)
已过时。 从 JDK 1.1 开始,由 Calendar.set(Calendar.MINUTE, int minutes) 取代。
void setMonth(int month)
已过时。 从 JDK 1.1 开始,由 Calendar.set(Calendar.MONTH, int month) 取代。
void setSeconds(int seconds)
已过时。 从 JDK 1.1 开始,由 Calendar.set(Calendar.SECOND, int seconds) 取代。
void setTime(long time)
设置此 Date 对象,以表示 1970 年 1 月 1 日 00:00:00 GMT 以后 time 毫秒的时间点。
void setYear(int year)
已过时。 从 JDK 1.1 开始,由 Calendar.set(Calendar.YEAR, year + 1900) 取代。
最好用Calendar类代替,更方便
③ Java生成大量按钮时如何做到瞬间加载
你可以用多线程来控制,等所有的按钮在后端加载后,最后那个显示的线程再进行
④ java 每秒运算
这个很简单啊,我写的这个你看下
public class Test
{
public static void main(String args[])
{
int sum=0;
for(int i=1;i<=10;i++)
{
sum=sum+i;
try
{
Thread.sleep(1000);//1000ms是一秒,就是让cpu停止1秒钟,时间可以自己设定
}
catch(Exception e){e.printStackTrace();}
System.out.println("sum="+sum);
}
}
}
对于你补充:
不需要导入其他的东西,但是有一点需要注意,就是sleep可能会抛出异常 ,所以要进行异常处理
对于影响来说,主线程里的程序肯定会有影响 ,因为它停的是主线程
但是对于其它的线程没有影响
⑤ 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());
}
}
⑥ java 时间相加问题
publicclassClock{
privateinthour;
privateintmin;
privateintsecond;
publicClock(inthour,intmin,intsecond){
this.hour=hour;
this.min=min;
this.second=second;
}
publicvoidadd(Clockclock){
intse=this.getSecond()+clock.getSecond();
intmin=this.getMin()+clock.getMin();
inthour=this.getHour()+clock.getHour();
while(se>=60){//如果秒数满60则-60秒,加一分钟
se=se-60;
min=min+1;
}
while(min>=60){
min=min-60;
hour=hour+1;
}
while(hour>=24){
hour=hour-24;
}
this.setSecond(se);
this.setMin(min);
this.setHour(hour);
}
publicintgetHour(){
returnhour;
}
publicvoidsetHour(inthour){
this.hour=hour;
}
publicintgetMin(){
returnmin;
}
publicvoidsetMin(intmin){
this.min=min;
}
publicintgetSecond(){
returnsecond;
}
publicvoidsetSecond(intsecond){
this.second=second;
}
publicStringformatedTime(){
return(this.getHour()>10?this.getHour():"0"+this.getHour())
+":"
+(this.getMin()>10?this.getMin():"0"+this.getMin())
+":"
+(this.getSecond()>10?this.getSecond():"0"
+this.getSecond());
}
publicstaticvoidmain(String[]args){
Clockclock=newClock(19,59,59);
ClockanotherClock=newClock(0,0,1);
clock.add(anotherClock);
System.out.println(clock.formatedTime());
}
}
//其中三目运算只是为了保证小于10的前面加个0,符合时间显示规范
⑦ JAVA如何给原来的时间加上个毫秒
2010-01-10 19:13:31.XXX
用字符串来做就行了,只要格式和上面的一样就可以了
⑧ java 两个时间段计算
两个时间段四个时间点,相当于时间轴上的两条线段(b代表起点,e代表端点,b<=e)和4个端点。
可分3种情况:
1.不相交。(b1-----e1)【b2-----e2】(b1-----e1)。if(e1<b2||b1>e2)此时,重合天数为零。
2.相交。
情况一:(b1---【b2---e1)----e2】 if(b1<b2&&e1<e2&&e1>b2)
情况二:【b2---(b1---e2】----e1) if(b1>b2&&b1<e2&&e2<e1)
3.包含:计算较短的时间段日期长度。
(b1---【b2-----e2】--e1) if(b1<b2&&e1>e2)
【b2---(b1-----e1)--e2】 if(b1>b2&&e1<e2)
实现代码如下:
[java] view plain
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* @author skysnow
*
*/
public class myDateUtil {
/**
*这里共有2个时间段(b1-----e1)【b2-----e2】,4个时间点;
*相当于两条线段(b代表起点,e代表端点,b<=e),4个端点。
*可分3种情况:
*1.不相交。(b1-----e1)【b2-----e2】(b1-----e1)。if(e1<b2||b1>e2)此时,重合天数为零。
*2.相交。
*情况一:(b1---【b2---e1)----e2】 if(b1<b2&&e1<e2&&e1>b2)
*情况二:【b2---(b1---e2】----e1) if(b1>b2&&b1<e2&&e2<e1)
*3.包含:计算较短的时间段日期长度。
*(b1---【b2-----e2】--e1) if(b1<b2&&e1>e2)
*【b2---(b1-----e1)--e2】 if(b1>b2&&e1<e2)
* @param begindate1 开始日期
* @param enddate1 结束日期
* @param begindate2开始日期
* @param enddate2 结束日期
* @return
*/
public static String getDayCoincidence(Date begindate1,Date enddate1,Date begindate2,Date enddate2){
long b1=begindate1.getTime();
long e1=enddate1.getTime();
long b2=begindate2.getTime();
long e2=enddate2.getTime();
assert(b1<e1&&b2<e2);
String coincidenceday;
if(b1<=b2&&e1>=e2){//(b1---【b2-----e2】--e1)
System.out.println("1包含2");
coincidenceday=getDayDifference(enddate2,begindate2);
}else if(b1>=b2&&e1<=e2){//【b2---(b1-----e1)--e2】
System.out.println("2包含1");
coincidenceday=getDayDifference(enddate1,begindate1);
}else if(b1>=b2&&b1<=e2&&e2<=e1){//【b2---(b1---e2】----e1)
System.out.println("相交");
coincidenceday=getDayDifference(enddate2,begindate1);
}else if(b1<=b2&&e1<=e2&&e1>=b2){//(b1---【b2---e1)----e2】
System.out.println("相交");
coincidenceday=getDayDifference(enddate1,begindate2);
}else if(e1<=b2||b1>=e2){
coincidenceday="0";
}else{
coincidenceday="";
System.out.println("意料外的日期组合,无法计算重合天数!");
}
System.out.println("重合天数为["+coincidenceday+"]天。");
return coincidenceday;
}
/**
* 计算两个日期的相差天数(d1-d2)
* @param d1
* @param d2
* @return
*/
public static String getDayDifference(Date d1,Date d2){
StringBuffer ds = new StringBuffer();
try{
long num = (d1.getTime()-d2.getTime())/1000;
long days = num/(3600*24);
if(days>=0)ds.append(days);
}catch(Exception e){
ds=new StringBuffer("");
e.printStackTrace();
}
return ds.toString();
}
public static Date stringToDate(String strDate) {
if (strDate==null){return null;}
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
public static String getThisMonth()
{
// 本月的第一天
Calendar calendar = new GregorianCalendar();
calendar.set(Calendar.DATE, 1);
SimpleDateFormat simpleFormate = new SimpleDateFormat("yyyy-MM-dd");
String fd = simpleFormate.format(calendar.getTime());
// 本月的最后一天
calendar.set( Calendar.DATE, 1 );
calendar.roll(Calendar.DATE, - 1 );
String ld = simpleFormate.format(calendar.getTime());
return fd+","+ld;
}
public static void main(String[] args) {
String[] thisMonth=getThisMonth().split(",");
Date begindate1 = stringToDate(thisMonth[0]+" 00:05:00");
Date enddate1 = stringToDate(thisMonth[0]+" 24:05:00");;
Date begindate2 = stringToDate(thisMonth[0]+" 00:05:00");
Date enddate2 = stringToDate(thisMonth[1]+" 00:00:00");
System.out.println(getDayCoincidence(begindate1, enddate1, begindate2, enddate2));
}
}
⑨ 用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();
}
}
}
}
⑩ 在此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();
}
}