新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

JAVA时间换算的代码 java时间换算的代码有哪些

用java写一个程序把24小时制的时间转换为12小时制的时间.具体说明内详

import java.util.Scanner;

创新互联主打移动网站、成都做网站、网站建设、网站改版、网络推广、网站维护、域名与空间、等互联网信息服务,为各行业提供服务。在技术实力的保障下,我们为客户承诺稳定,放心的服务,根据网站的内容与功能再决定采用什么样的设计。最后,要实现符合网站需求的内容、功能与设计,我们还会规划稳定安全的技术方案做保障。

public class TimeFormatException extends Exception {

public void printException() {

System.out.println("输入时间错误!!程序结束");

}

public TimeFormatException() {

}

public void printDate() throws TimeFormatException {

boolean bStop = true;

Scanner input = new Scanner(System.in);

String reg = "([0-1][0-9]|2[0-4]):([0-5][0-9])";

while (bStop) {

System.out.println("请输入时间:");

String str = input.next();

if (str.matches(reg)) {

int hour = Integer.parseInt(str.substring(0, 2));

String minutes = str.substring(2, 5);

if (hour  12)

System.out.println("现在时间是:" + Integer.toString(hour).concat(minutes) + " am");

else if (hour == 12)

System.out.println("现在时间是:" + Integer.toString(hour).concat(minutes) + " pm");

else if(hour == 24)

System.out.println("现在时间是:" + "00".concat(minutes) + " am");

else

System.out.println("现在时间是:" + Integer.toString(hour - 12).concat(minutes) + " pm");

} else {

bStop = false;

throw new TimeFormatException();

}

}

}

public static void main(String[] args) {

try {

new TimeFormatException().printDate();

} catch (TimeFormatException e) {

e.printException();

}

}

}

如果看不懂 尽管问 ch_felix168_88@163.com

将java怎么将long类型的时间转换成年月日的形式

用java代码实现:

public static String longToDate(long lo){ 

Date date = new Date(lo); 

SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  

return sd.format(date);

}

资料拓展:

长整型(long)是计算机程序语言,是程序设计中数据类型的一种表现方式,一般情况下用long 表示长整型。 long 有符号64位整数,范围是-2^63-2^63 -1 Int64。

Java计算两个日期时间相差几天,几小时,几分钟等

思路是先计算两个日期相差的毫秒数,然后分别根据每天的毫秒数、每小时的毫秒数、每分钟的毫秒数来计算相差几天,几小时,几分钟。具体代码如下:

public static String getDatePoor(Date endDate, Date nowDate) {

long nd = 1000 * 24 * 60 * 60;//每天毫秒数

long nh = 1000 * 60 * 60;//每小时毫秒数

long nm = 1000 * 60;//每分钟毫秒数

long diff = endDate.getTime() - nowDate.getTime(); // 获得两个时间的毫秒时间差异

long day = diff / nd;   // 计算差多少天

long hour = diff % nd / nh; // 计算差多少小时

long min = diff % nd % nh / nm;  // 计算差多少分钟

return day + "天" + hour + "小时" + min + "分钟";

}

然后做一个测试,调用这个方法测试一下:

可以看出两个日期时间相差几天,几小时,几分钟都可以算出来。

扩展资料:

Java使用以下三种方法来比较两个日期:

1、使用 getTime() 方法获取两个日期(自1970年1月1日经历的毫秒数值),然后比较这两个值。

2、使用方法 before(),after() 和 equals()。例如,一个月的12号比18号早,则 new Date(99, 2, 12).before(new Date (99, 2, 18)) 返回true。

3、使用 compareTo() 方法,它是由 Comparable 接口定义的,Date 类实现了这个接口。

参考资料:

Oracle官方API接口-Util-Date

Oracle官方API接口-SimpleDateFormat

菜鸟教程-Java 日期时间

java整么计算时间

提供一个时间操作类给你,你可以调用里面的dateDiff方法,dateDiff(new Date(), getDateFromString("明天的字符串")表示的就是现在到明天0点之前的差距天数。

你要分钟数的话就自己改一下dateDiff,不要除以60和24就是分钟了

以下是工具类代码

import org.apache.log4j.Logger;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

public class AdDateUtil {

private static Logger logger = Logger.getLogger(AdDateUtil.class);

static public String getNowStr(String format) {

SimpleDateFormat sdf = new SimpleDateFormat(format);

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

return now;

}

static public Date getFormatDate(String date, String format) {

SimpleDateFormat sdf = new SimpleDateFormat(format);

Date d = new Date();

try {

d = sdf.parse(date);

} catch (ParseException e) {

logger.error(e);

}

return d;

}

static public String getDateStr(Date date, String format) {

SimpleDateFormat sdf = new SimpleDateFormat(format);

String d = sdf.format(date);

return d;

}

static public String getPadZeroString(String s, int size) {

StringBuffer sb = new StringBuffer();

for (int i = 0; i  (size - s.length()); i++) {

sb.append("0");

}

sb.append(s);

return sb.toString();

}

/**

* 得到某月的天数

*

* @param year

* @param month

* @return

*/

static public int getDayCountOfMonth(String year, String month) {

Calendar cal = Calendar.getInstance();

// 年

cal.set(Calendar.YEAR, Integer.parseInt(year));

// 月,因为Calendar里的月是从0开始,所以要-1

cal.set(Calendar.MONTH, Integer.parseInt(month) - 1);

return cal.getActualMaximum(Calendar.DAY_OF_MONTH);

}

static public String getYesterday(String format) {

SimpleDateFormat df = new SimpleDateFormat(format);

Calendar now = Calendar.getInstance();

now.roll(Calendar.DAY_OF_YEAR, -1); //昨天

return df.format(now.getTime());

}

/**

* 获取和今天附近的某天

* @param format

* @param diff

* @return

*/

static public String getADay(String format, int diff) {

SimpleDateFormat df = new SimpleDateFormat(format);

Calendar now = Calendar.getInstance();

int beforeM = now.get(Calendar.MONTH);

now.roll(Calendar.DAY_OF_YEAR, diff); //

int nowM = now.get(Calendar.MONTH);

//必须进行日期处理,否则2009-01-04日前七天是2009-12-28

if (nowM  beforeM) {

now.roll(Calendar.YEAR, -1);

}

return df.format(now.getTime());

}

static public String getTomorrow(String format) {

SimpleDateFormat df = new SimpleDateFormat(format);

Calendar now = Calendar.getInstance();

now.roll(Calendar.DAY_OF_YEAR, 1); //明天

return df.format(now.getTime());

}

/**

* 得到最近num天的全部日期

* 说明:

* 1.日期是从昨天开始算的.

* 2.如果num=2 , 日期是2008-03-14 ,则返回的结果为 2008-03-12、2008-03-13

* @param num

* @return

*/

public static String[] getDaysByNum(int num, String date) {

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

String[] result = {  };

Calendar cal = Calendar.getInstance();

cal.setTime(getDateFromString(date, "yyyy-MM-dd"));

//最近一周

result = new String[num];

for (int i = num; i  0; i--) {

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

result[i - 1] = sdf.format(new Date(cal.getTimeInMillis()));

}

return result;

}

public static Date getDateFromString(String dateStr, String format) {

if ((dateStr == null) || (format == null)) {

try {

throw new Exception("数据类型异常" + dateStr + "|" + format);

} catch (Exception e) {

logger.error("数据类型异常:" + e);

}

}

SimpleDateFormat df = new SimpleDateFormat(format);

Date date;

try {

date = df.parse(dateStr);

return date;

} catch (Exception ex) {

logger.error(ex);

return new Date();

}

}

static public int getNowYear() {

Calendar cal = Calendar.getInstance();

return cal.get(Calendar.YEAR);

}

static public int getNowMonth() {

Calendar cal = Calendar.getInstance();

return cal.get(Calendar.MONTH) + 1;

}

public static String[] getMonthRang(String year, String month) {

String beginDate = year + "-" + month + "-01";

String endDate = year + "-" + month + "-" +

getDayCountOfMonth(year, month);

return getDaysByRang(beginDate, endDate);

}

public static String[] getDaysByRang(String beginDate, String endDate) {

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

//得到两个日期间相差多少天

int num = dateDiff(beginDate, endDate);

if (num  0) {

//颠倒一下日期

String tmp = beginDate;

beginDate = endDate;

endDate = tmp;

num = 0 - num;

}

String[] result = {  };

Calendar cal = Calendar.getInstance();

try {

cal.setTime(sdf.parse(beginDate));

} catch (ParseException e) {

e.printStackTrace();

}

num = num + 1; //把开始和结束日期都包含进去

result = new String[num];

for (int i = 0; i  num; i++) {

if (i  0) {

cal.add(Calendar.DAY_OF_YEAR, 1);

}

result[i] = sdf.format(new Date(cal.getTimeInMillis()));

}

return result;

}

public static int dateDiff(String beginDate, String endDate) {

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

Date date = null;

try {

date = sdf.parse(endDate);

} catch (ParseException e) {

date = new Date();

e.printStackTrace();

}

long end = date.getTime();

try {

date = sdf.parse(beginDate);

} catch (ParseException e) {

date = new Date();

e.printStackTrace();

}

long begin = date.getTime();

long day = (end - begin) / (1000 * 3600 * 24); //除1000是把毫秒变成秒

return Integer.parseInt(Long.toString(day));

}

public static void main(String[] args) {

System.out.println(AdDateUtil.getADay("yyyy-MM-dd", -7));

}

}


文章标题:JAVA时间换算的代码 java时间换算的代码有哪些
当前链接:http://www.wjwzjz.com/article/doiggsg.html
在线咨询
服务热线
服务热线:028-86922220
TOP