时间段的java代码 java中时间

java 两个时间段计算

两个时间段四个时间点,相当于时间轴上的两条线段(b代表起点,e代表端点,b=e)和4个端点。

创新互联一直秉承“诚信做人,踏实做事”的原则,不欺瞒客户,是我们最起码的底线! 以服务为基础,以质量求生存,以技术求发展,成交一个客户多一个朋友!为您提供网站设计、成都网站设计、成都网页设计、成都小程序开发、成都网站开发、成都网站制作、成都软件开发、成都app软件开发公司是成都本地专业的网站建设和网站设计公司,等你一起来见证!

可分3种情况:

1.不相交。(b1-----e1)【b2-----e2】(b1-----e1)。if(e1b2||b1e2)此时,重合天数为零。

2.相交。

情况一:(b1---【b2---e1)----e2】 if(b1b2e1e2e1b2)

情况二:【b2---(b1---e2】----e1) if(b1b2b1e2e2e1)

3.包含:计算较短的时间段日期长度。

(b1---【b2-----e2】--e1) if(b1b2e1e2)

【b2---(b1-----e1)--e2】 if(b1b2e1e2)

实现代码如下:

[java] view plaincopy

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(e1b2||b1e2)此时,重合天数为零。

*2.相交。

*情况一:(b1---【b2---e1)----e2】 if(b1b2e1e2e1b2)

*情况二:【b2---(b1---e2】----e1) if(b1b2b1e2e2e1)

*3.包含:计算较短的时间段日期长度。

*(b1---【b2-----e2】--e1) if(b1b2e1e2)

*【b2---(b1-----e1)--e2】 if(b1b2e1e2)

* @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(b1e1b2e2);

String coincidenceday;

if(b1=b2e1=e2){//(b1---【b2-----e2】--e1)

System.out.println("1包含2");

coincidenceday=getDayDifference(enddate2,begindate2);

}else if(b1=b2e1=e2){//【b2---(b1-----e1)--e2】

System.out.println("2包含1");

coincidenceday=getDayDifference(enddate1,begindate1);

}else if(b1=b2b1=e2e2=e1){//【b2---(b1---e2】----e1)

System.out.println("相交");

coincidenceday=getDayDifference(enddate2,begindate1);

}else if(b1=b2e1=e2e1=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获取一段时间代码

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

public class WhatTest {

public static void main(String[] args) {

Calendar cal = Calendar.getInstance();

Date now = cal.getTime();

cal.add(Calendar.MONTH, -1);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00---yyyy-MM-dd 23:59:59");

while (cal.getTime().before(now)) {

System.out.println(sdf.format(cal.getTime()));

cal.add(Calendar.DAY_OF_YEAR, 1);

}

}

}

如何用java实现将一天的7:30-22:05按照下面的时间段分成13段,判断当前时间在哪一段时间里?

你可以用一个字符串类型的数组将13节存入其中,然后循环比较。比较的时候要将字条串用split(“-”)先分割开,再分别转为Date类型。假如d1是起始时间,d2是结束时间,当前时间是d3,d3.after(d1)为true,d3.before(d2)为true时,当前时间就是这一节中,循环变量i+1是第几节。

望采纳

以下是实现方法:我只举了三个时间段

String[] dates = new String[3];

dates[0] = "1:00-8:00";

dates[1] = "8:00-16:00";

dates[2] = "16:00-24:00";

for (int i = 0; i dates.length; i++) {

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");

String currentDate = sdf.format(new Date());

String dateStr = dates[i];

String[] dateArr = dateStr.split("-");

try {

Date currDate = sdf.parse(currentDate);//当前时间

Date startDate = sdf.parse(dateArr[0]);//每节开始时间

Date endDate = sdf.parse(dateArr[1]);//每节结束时间

if(currDate.after(startDate) currDate.before(endDate)){

System.out.println("当前时间属于第"+(i+1)+"节");

}

} catch (ParseException e) {

e.printStackTrace();

}

}

}

一段显示当下时间的JAVA代码

private Shape rect;            //背景矩形

private Font font;            //设置字体

private Date date;            //现在的时间

private Thread time;        //时间线程

private CanvasPanel canvas;

public static void main(String[] args) {

new TimerTest20140930();

}

public TimerTest20140930(){

super("绘制文本");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(400,300);

rect = new Rectangle2D.Double(10,10,200,100);

font = new Font("宋体",Font.BOLD,16);

canvas=new CanvasPanel();

add(canvas);

time = new Thread(new Runnable(){

public void run(){

while(true){

canvas.repaint();

try{

Thread.sleep(1000);

}catch(Exception ex){

}

}

}

});

time.start();

setLocationRelativeTo(null);

setVisible(true);

}  

class CanvasPanel extends Canvas {

public void paint(Graphics g){

super.paint(g);

Graphics2D g2 = (Graphics2D) g;

g2.setColor(Color.BLACK);

g2.fill(rect);

g2.setColor(Color.BLUE);

g2.setFont(font);

g2.drawString("现在的时间是", 20, 30);

date = new Date();

g2.drawString(String.format("%tr", date), 50, 60);       

}

}

java怎么加时间段

public class Test1 {

public static void main(String[] args) {

// java 代码如何获取当前时间的上一个月的月末时间..

Calendar cal = Calendar.getInstance();

// 设置天数为-1天,表示当月减一天即为上一个月的月末时间

cal.set(Calendar.DAY_OF_MONTH, -1);

//格式化输出年月日

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd 00:00:00");

System.out.println(sdf.format(cal.getTime()));

}

}

写一个JAVA程序,例如每天上午8点,晚上8点调用一个方法,如何实现?

猜数,电脑随机产生一个1-10之间的数,你来猜是几,猜中后,输入n或n退出,其他继续猜。

public

static

int

getintinrange(string

prompt)方法

输入一个1-10之间的数,如果不在这个范围,重新输入

网站题目:时间段的java代码 java中时间
链接分享:https://www.cdcxhl.com/article18/doohgdp.html

成都网站建设公司_创新互联,为您提供网站设计微信小程序虚拟主机企业网站制作建站公司小程序开发

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都网站建设公司