资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

用java代码写美国时间 java时间格式转换代码

java程序怎么把美式时间转换为国际时间格式 美式:august 8.2008 国际:8 aug

把美式时间转换为国际时间格式的Java程序如下

创新互联是工信部颁发资质IDC服务器商,为用户提供优质的南充服务器托管服务

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Locale;

public class BB {

public static void main(String[] args) {

String s="August 8.2008";

SimpleDateFormat sdf1=new SimpleDateFormat("MMMMM d.yyyy",Locale.US);

Date d = null;

try {

d = sdf1.parse(s);

} catch (ParseException e) {

e.printStackTrace();

}

SimpleDateFormat sdf2=new SimpleDateFormat("d MMMMM yyyy",Locale.UK);

System.out.println(sdf2.format(d));

}

}

运行结果

8 August 2008

java代码怎样获取internet标准时间?

获取internet标准时间,参考以下代码:

TimeZone.setDefault(TimeZone.getTimeZone("GMT+8")); // 时区设置  

URL url=new URL("

);//取得资源对象

URLConnection uc=url.openConnection();//生成连接对象  

uc.connect(); //发出连接  

long ld=uc.getDate(); //取得网站日期时间(时间戳)  

Date date=new Date(ld); //转换为标准时间对象  

//分别取得时间中的小时,分钟和秒,并输出  

System.out.print(date.getHours()+"时"+date.getMinutes()+"分"+date.getSeconds()+"秒");

java script时间代码

下面这个javascript是每秒显示一次时间,你只需要把下面的showTime()函数里面程序一修改就可以得到你想要的结果了

setInterval("showTime()", 1000);

function showTime()

{

var today = new Date();

alert("The time is: " + today.toString());

} 答案补充 setInterval("showTime()", 1000);

function showTime()

{

//你把这个时间变成你想要的就可以了啊,然后每过一秒时间就加一秒

var today = new Date(2008,11,30,11,20,45);

alert("The time is: " + today.toString());

} 答案补充 获取日期的时间方法

 getYear(): 返回年数

 getMonth():返回当月号数

 getDate(): 返回当日号数

 getDay():返回星期几

 getHours():返回小时数

 getMintes(:返回分钟数

 getSeconds():返回秒数

 getTime() : 返回毫秒数

设置日期和时间:

 setYear();设置年

 setDate():设置当月号数

 setMonth():设置当月份数

 setHours():设置小时数

 setMintes():设置分钟数

 setSeconds():设置秒数

 setTime ():设置毫秒数

例子:

var d = new Date("2008/11/30");

d.setMonth(d.getMonth() + 1 + 1);//加一个月,同理,可以加一天:getDate()+1,加一年:getYear()+1

编写一个java程序用以将AM/PM格式的时间转换为24小时格式,求大神完成代码

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

String strCurrentTime = objSDateFormat.format(Date类型的时间);

注:大写的HH为24小时制,小写的hh为12小时制,当然还可以在ss的后面加上 a,这样可以在后面显示上下文:显示效果为“2008-03-24 17:00:14 下午”

这个更全

实现思路就是输入一个时间,之后会输出相应的12小时和24小时效果展示:

import java.text.SimpleDateFormat;

import java.util.Locale;

import java.util.Scanner;

public class App {

public static void main(String[] args) {

while (true) {

System.out.println("Enter time in 24-hour notation:");

Scanner sc = new Scanner(System.in);

String line = sc.nextLine();

try {

outTime(line);

} catch (TimeFormatException e) {

System.out.println("There is no such time as " + line);

System.out.println("Try again:");

continue;

}

sc = new Scanner(System.in);

line = sc.nextLine();

if ("n".equalsIgnoreCase(line)) {

break;

}

}

System.out.println("End of program");

}

public static void outTime(String line) throws TimeFormatException {

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

SimpleDateFormat _12time = new SimpleDateFormat("hh:mm a",

Locale.ENGLISH);

try {

String[] array = line.split(":");

if (Integer.parseInt(array[0]) 0

|| Integer.parseInt(array[0]) 23) {

throw new TimeFormatException();

}

if (Integer.parseInt(array[1]) 0

|| Integer.parseInt(array[1]) 59) {

throw new TimeFormatException();

}

System.out.println(_12time.format(_24time.parse(line)));

System.out.println("Again?(y/n)");

} catch (Exception e) {

throw new TimeFormatException();

}

}

}

class TimeFormatException extends Exception {

}


网页名称:用java代码写美国时间 java时间格式转换代码
网页网址:http://cdkjz.cn/article/ddchggj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220