资讯

精准传达 • 有效沟通

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

java设置时区,Java中如何将时间设置为12小时制并区分上下

成都创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:网站设计、做网站、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的潞州网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

创新互联公司于2013年创立,先为达州等服务建站,达州等地企业,进行企业商务咨询服务。为达州企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

本文目录一览

1,Java中如何将时间设置为12小时制并区分上下

首先是小时取出,然后就是判断:1. 当小时是大于等于12,就是下午;2. 当小于12 ,就是上午;;3.

java中将12小时制的时间转换为24小时制的方式如下: import java.text.simpledateformat; import java.util.date; public class ceshi { public static void main(string[] args) { simpledateformat objsdateformat = new simpledateformat( "yyyy-mm-dd hh:mm:ss");//转换为24小时制 string strcurrenttime = objsdateformat.format(new date()); system.out.println(strcurrenttime); } }注:大写的hh为24小时制,小写的hh为12小时制,当然还可以在ss的后面加上 a,这样可以在后面显示上下文:显示效果为“2008-03-24 17:00:14 下午” 运行结果为:

2,java DateFormat类的setTimeZoneTimeZone zone怎样使用


123456setTimeZonepublic void setTimeZone(TimeZone zone)为此 DateFormat 对象的日历设置时区。参数:zone - 给定的新时区。TimeZone12345678getDefaultpublic static TimeZone getDefault()获取此主机的默认 TimeZone。默认 TimeZone 的来源可能随实现的变化而变化。返回:默认的 TimeZone。另请参见:setDefault(java.util.TimeZone)12345678getTimeZonepublic static TimeZone getTimeZone(String ID)获取给定 ID 的 TimeZone。参数:ID - TimeZone 的 ID,要么是缩写(如 "PST" ),要么是全名(如 "America/Los_Angeles"),要么是自定义 ID(如 "GMT-8:00")。注意,对缩写的支持只是出于 JDK 1.1.x 兼容性的考虑,因此应该使用全名。返回:指定的 TimeZone,如果给定的 ID 无法理解,则返回 GMT 区域。
1234 DateFormat dateFormat = new SimpleDateFormat(); //dateFormat.setTimeZone(TimeZone.getDefault()); dateFormat.setTimeZone(TimeZone.getTimeZone("EET")); System.out.println(dateFormat.format(new Date()));

3,JAVA环境时间调整

你的这个问题系 系统 JVM的事,可以设置下时区,通过以下的语句:import java.util.*;TimeZone tz = TimeZone.getTimeZone("ETC/GMT-8");TimeZone.setDefault(tz);//显示Java能识别的系统中所有正确的时区/**String[] ids = TimeZone.getAvailableIDs();for(int i = 0; i < ids.length; i++)System.out.println(ids[i].toString());*/我也是复制的 ,你自己看看吧!!

为了使使用jdk更方便。你看教程里编译运行程序只需打javac test.javajava test如果你不设环境变量,这些命令就得这么打了c:\program files\java\jdk1.6.0_10\bin\javac test.javac:\program files\java\jdk1.6.0_10\bin\java test很麻烦吧你可以在qq里以java为关键字搜索。最好进标明容纳新人的群,不然很容易被t出来。good luck.

4,关于JAVA时间格式转换问题涉及时区

//我实现一个时钟窗口程序给你了,好让你更容易理解,希望对你有帮助。import java.awt.*; import java.awt.event.*; import java.util.*; //世界时钟 public class TimerTest { public static void main(String[] args) { new TimerTestFrame("世界时钟"); } } class TimerTestFrame extends Frame { /** * */ private static final long serialVersionUID = 1L; public TimerTestFrame(String s) { super(s); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { dispose(); System.exit(0); } }); setLayout(new GridLayout(2, 6)); ClockCanvas clk1 = new ClockCanvas("北京", "GMT+8"); // 创建时钟 ClockCanvas clk2 = new ClockCanvas("巴黎", "GMT+2"); ClockCanvas clk3 = new ClockCanvas("华盛顿", "GMT-4"); ClockCanvas clk4 = new ClockCanvas("洛衫矶", "GMT-7"); ClockCanvas clk5 = new ClockCanvas("伦敦", "GMT+1"); ClockCanvas clk6 = new ClockCanvas("芝加哥", "GMT-5"); add(clk1); add(clk2); add(clk3); add(clk4); add(clk5); add(clk6); setSize(500, 350); // 设置框架宽高 setVisible(true); } } class ClockCanvas extends Canvas implements Runnable { /** * */ private static final long serialVersionUID = 1L; private int seconds = 0; private String city; private GregorianCalendar calendar; Thread t; public ClockCanvas(String c, String tz) { city = c; //也可以通过TimeZone.setTimeZone(String n)函数改变时区,n为时区参数名。calendar = new GregorianCalendar(TimeZone.getTimeZone(tz)); t = new Thread(this); t.start(); setSize(125, 125); // 设置画布大小 setBackground(Color.black); } // 重写父类的方法绘制时钟图形 public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; BasicStroke bstroke = new BasicStroke(2.0f); g2d.setStroke(bstroke); g2d.setColor(Color.green); g2d.drawOval(0, 0, 100, 100); bstroke = new BasicStroke(5.0f); g2d.setStroke(bstroke); g2d.drawLine(50, 0, 50, 5); g2d.drawLine(0, 50, 5, 50); g2d.drawLine(50, 95, 50, 98); g2d.drawLine(95, 50, 98, 50); double hourAngle = 2 * Math.PI * (seconds - 3 * 60 * 60) / (12 * 60 * 60); double minuteAngle = 2 * Math.PI * (seconds - 15 * 60) / (60 * 60); double secondAngle = 2 * Math.PI * (seconds - 15) / (60); bstroke = new BasicStroke(5.0f); g2d.setStroke(bstroke); g2d.drawLine(50, 50, 50 + (int) (30 * Math.cos(hourAngle)), 50 + (int) (30 * Math.sin(hourAngle))); bstroke = new BasicStroke(3.0f); g2d.setStroke(bstroke); g2d.drawLine(50, 50, 50 + (int) (40 * Math.cos(minuteAngle)), 50 + (int) (40 * Math.sin(minuteAngle))); bstroke = new BasicStroke(1.0f); g2d.setStroke(bstroke); g2d.drawLine(50, 50, 50 + (int) (45 * Math.cos(secondAngle)), 50 + (int) (45 * Math.sin(secondAngle))); g2d.setColor(Color.red); g2d.drawString(city, 35, 115); } public void timeElapsed() { //new Date()获得当前时间 System.out.println(new Date()); calendar.setTime(new Date()); seconds = calendar.get(Calendar.HOUR) * 60 * 60 + calendar.get(Calendar.MINUTE) * 60 + calendar.get(Calendar.SECOND); } public void run() { try { while (true) { Thread.sleep(300); timeElapsed(); repaint(); } } catch (InterruptedException e) { } } }

5,java中如何设置时间和显示时间

System.currentmils()获取从格林威治时间到当前的毫秒数。然后你去书上查下吧 具体的我不记得了。

JAVA中获取当前系统时间2011-07-06 20:45 并格式化输出:import java.util.Date;import java.text.SimpleDateFormat;public class NowString public static void main(String[] args) SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式 System.out.println(df.format(new Date()));// new Date()为获取当前系统时间 }}设置时间,推荐 使用java.util.Calendar类来进行操作,import java.util.Date;import java.util.Calendar;import java.text.SimpleDateFormat;public class TestDatepublic static void main(String[] args)Date now = new Date();SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//可以方便地修改日期格式String hehe = dateFormat.format( now );System.out.println(hehe);Calendar c = Calendar.getInstance();//可以对每个时间域单独修改int year = c.get(Calendar.YEAR);int month = c.get(Calendar.MONTH);int date = c.get(Calendar.DATE);int hour = c.get(Calendar.HOUR_OF_DAY);int minute = c.get(Calendar.MINUTE);int second = c.get(Calendar.SECOND);System.out.println(year + "/" + month + "/" + date + " " +hour + ":" +minute + ":" + second);}}

用qq直接开启视频就好了啊。。

6,在java里面如何得到UTC时间 时间格式为Tue Oct 12 000000

你的0800表示的是时区,也有这样的(new Date()).toUTCString()我问了几个人,他们都说是js的

public class stringtodate public static void main(string []args) string mystring="2011-09-18 11:20:30"; date mydate=null; dateformat df = dateformat.getdateinstance(); //设置时间格式 simpledateformat myformdate = new simpledateformat("yyyy-mm-dd hh:mm:ss"); try mydate = df.parse(mystring); } catch (parseexception e) // todo auto-generated catch block e.printstacktrace(); }system.out.println(mydate);}}结果:sun sep 18 00:00:00 cst 2011

Calendar gc = GregorianCalendar.getInstance(); cal.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset)); //之后调用cal.get(int x)或cal.getTimeInMillis()方法所取得的时间即是UTC标准时间。System.out.println("UTC:"+new Date(cal.getTimeInMillis())); 赠送其它时间方法,总有一款适合您public static void main(String[] args) SimpleDateFormat foo = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); System.out.println("foo:"+foo.format(new Date())); Calendar gc = GregorianCalendar.getInstance(); System.out.println("gc.getTime():"+gc.getTime()); System.out.println("gc.getTimeInMillis():"+new Date(gc.getTimeInMillis())); //当前系统默认时区的时间: Calendar calendar=new GregorianCalendar(); System.out.print("时区:"+calendar.getTimeZone().getID()+" "); System.out.println("时间:"+calendar.get(Calendar.HOUR_OF_DAY)+":"+calendar.get(Calendar.MINUTE)); //美国洛杉矶时区 TimeZone tz=TimeZone.getTimeZone("America/Los_Angeles"); //时区转换 calendar.setTimeZone(tz); System.out.print("时区:"+calendar.getTimeZone().getID()+" "); System.out.println("时间:"+calendar.get(Calendar.HOUR_OF_DAY)+":"+calendar.get(Calendar.MINUTE)); Date time=new Date(); //1、取得本地时间: java.util.Calendar cal = java.util.Calendar.getInstance(); //2、取得时间偏移量: int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET); //3、取得夏令时差: int dstOffset = cal.get(java.util.Calendar.DST_OFFSET); //4、从本地时间里扣除这些差量,即可以取得UTC时间: cal.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset)); //之后调用cal.get(int x)或cal.getTimeInMillis()方法所取得的时间即是UTC标准时间。System.out.println("UTC:"+new Date(cal.getTimeInMillis())); Calendar calendar1 = Calendar.getInstance(); TimeZone tztz = TimeZone.getTimeZone("GMT"); calendar1.setTimeZone(tztz); System.out.println(calendar.getTime()); System.out.println(calendar.getTimeInMillis()); } 运算结果是Tue Oct 19 16:54:57 CST 2010 符合你的要求只是以毫秒来算的

7,Java时间格式转换如何获得时区

//我实现一个时钟窗口程序给你了,好让你更容易理解,希望对你有帮助。import java.awt.*;import java.awt.event.*;import java.util.*;//世界时钟public class TimerTest public static void main(String[] args) new TimerTestFrame("世界时钟"); }}class TimerTestFrame extends Frame /** * */ private static final long serialVersionUID = 1L; public TimerTestFrame(String s) super(s); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) dispose(); System.exit(0); } }); setLayout(new GridLayout(2, 6)); ClockCanvas clk1 = new ClockCanvas("北京", "GMT+8"); // 创建时钟 ClockCanvas clk2 = new ClockCanvas("巴黎", "GMT+2"); ClockCanvas clk3 = new ClockCanvas("华盛顿", "GMT-4"); ClockCanvas clk4 = new ClockCanvas("洛衫矶", "GMT-7"); ClockCanvas clk5 = new ClockCanvas("伦敦", "GMT+1"); ClockCanvas clk6 = new ClockCanvas("芝加哥", "GMT-5"); add(clk1); add(clk2); add(clk3); add(clk4); add(clk5); add(clk6); setSize(500, 350); // 设置框架宽高 setVisible(true); }}class ClockCanvas extends Canvas implements Runnable /** * */ private static final long serialVersionUID = 1L; private int seconds = 0; private String city; private GregorianCalendar calendar; Thread t; public ClockCanvas(String c, String tz) city = c; //也可以通过TimeZone.setTimeZone(String n)函数改变时区,n为时区参数名。calendar = new GregorianCalendar(TimeZone.getTimeZone(tz)); t = new Thread(this); t.start(); setSize(125, 125); // 设置画布大小 setBackground(Color.black); } // 重写父类的方法绘制时钟图形 public void paint(Graphics g) Graphics2D g2d = (Graphics2D) g; BasicStroke bstroke = new BasicStroke(2.0f); g2d.setStroke(bstroke); g2d.setColor(Color.green); g2d.drawOval(0, 0, 100, 100); bstroke = new BasicStroke(5.0f); g2d.setStroke(bstroke); g2d.drawLine(50, 0, 50, 5); g2d.drawLine(0, 50, 5, 50); g2d.drawLine(50, 95, 50, 98); g2d.drawLine(95, 50, 98, 50); double hourAngle = 2 * Math.PI * (seconds - 3 * 60 * 60) / (12 * 60 * 60); double minuteAngle = 2 * Math.PI * (seconds - 15 * 60) / (60 * 60); double secondAngle = 2 * Math.PI * (seconds - 15) / (60); bstroke = new BasicStroke(5.0f); g2d.setStroke(bstroke); g2d.drawLine(50, 50, 50 + (int) (30 * Math.cos(hourAngle)), 50 + (int) (30 * Math.sin(hourAngle))); bstroke = new BasicStroke(3.0f); g2d.setStroke(bstroke); g2d.drawLine(50, 50, 50 + (int) (40 * Math.cos(minuteAngle)), 50 + (int) (40 * Math.sin(minuteAngle))); bstroke = new BasicStroke(1.0f); g2d.setStroke(bstroke); g2d.drawLine(50, 50, 50 + (int) (45 * Math.cos(secondAngle)), 50 + (int) (45 * Math.sin(secondAngle))); g2d.setColor(Color.red); g2d.drawString(city, 35, 115); } public void timeElapsed() //new Date()获得当前时间 System.out.println(new Date()); calendar.setTime(new Date()); seconds = calendar.get(Calendar.HOUR) * 60 * 60 + calendar.get(Calendar.MINUTE) * 60 + calendar.get(Calendar.SECOND); } public void run() try while (true) Thread.sleep(300); timeElapsed(); repaint(); } } catch (InterruptedException e) } }}


网站栏目:java设置时区,Java中如何将时间设置为12小时制并区分上下
网站路径:http://cdkjz.cn/article/ejieio.html
多年建站经验

多一份参考,总有益处

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

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

大客户专线   成都:13518219792   座机:028-86922220