资讯

精准传达 • 有效沟通

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

夏令时Java代码处理,java 夏令时

如何在linux下 使用java代码正确获取夏令时的时间

一:环境搭建

创新互联不只是一家网站建设的网络公司;我们对营销、技术、服务都有自己独特见解,公司采取“创意+综合+营销”一体化的方式为您提供更专业的服务!我们经历的每一步也许不一定是最完美的,但每一步都有值得深思的意义。我们珍视每一份信任,关注我们的网站建设、成都网站制作质量和服务品质,在得到用户满意的同时,也能得到同行业的专业认可,能够为行业创新发展助力。未来将继续专注于技术创新,服务升级,满足企业一站式营销型网站建设需求,让再小的成都品牌网站建设也能产生价值!

OpenOffice 下载地址

下载地址

解压后将目录下的所有jar包放在工程的lib下面或者采用引用的方式调用这些jar包。

下载后安装,我安装的路径为D:/openOffice/install/

二:启动服务

可以通过cmd调用服务, " cd D:/openOffice/install/program"

执行

soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

查看是否安装成功,查看端口对应的pid

netstat -ano|findstr 8100

查看pid对应的服务程序名

tasklist|findstr pid值

也可以把这一步省略,放到java程序中调用服务,因为启动服务占用内存比较大,在java中可以在使用

的时候调用,然后马上销毁。

三:程序代码

1:将word转换为pdf方法

1 // 将word格式的文件转换为pdf格式

2 public void Word2Pdf(String srcPath, String desPath) throws IOException {

3 // 源文件目录

4 File inputFile = new File(srcPath);

5 if (!inputFile.exists()) {

6 System.out.println("源文件不存在!");

7 return;

8 }

9 // 输出文件目录

10 File outputFile = new File(desPath);

11 if (!outputFile.getParentFile().exists()) {

12 outputFile.getParentFile().exists();

13 }

14 // 调用openoffice服务线程

15 String command = "D:/openOffice/install/program/soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"";

16 Process p = Runtime.getRuntime().exec(command);

17

18 // 连接openoffice服务

19 OpenOfficeConnection connection = new SocketOpenOfficeConnection(

20 "127.0.0.1", 8100);

21 connection.connect();

22

23 // 转换word到pdf

24 DocumentConverter converter = new OpenOfficeDocumentConverter(

25 connection);

26 converter.convert(inputFile, outputFile);

27

28 // 关闭连接

29 connection.disconnect();

30

31 // 关闭进程

32 p.destroy();

33 System.out.println("转换完成!");

34 }

2:调用方法

1 @Test

2 public void testWord2Pdf() throws IOException {

3 String srcPath = "E:/test.docx";

4 String desPath = "E:/test.pdf";

5 Word2Pdf(srcPath, desPath);

6 }

以上代码经过验证,可以正常运行。

四:遇到问题

错误信息:

java.net.ConnectException: connection failed: socket,host=10.101.50.71,port=8100,tcpNoDelay=1: java.net.ConnectException: Connection refused: connect

at com.artofsolving.jodconverter.openoffice.connection.AbstractOpenOfficeConnection.connect(AbstractOpenOfficeConnection.java:79)

原因以及解决方法:第一次调用,soffice需要注册,所以到soffice.exe的安装路径下双击soffice.exe,注册即可。

Java编程

import java.util.Scanner;

public class Calendar {

public static void main(String[] args) {

System.out.println("******************欢 迎 使 用 万 年 历******************");

Scanner input = new Scanner(System.in);

System.out.print("\n请选择年份: ");

int year = input.nextInt();

System.out.print("\n请选择月份: ");

int month = input.nextInt();

System.out.println();

int days = 0; // 存储当月的天数

/* 计算输入的年份之前的天数 */

int totalDays = 0;

for (int i = 1900; i year; i++) {

/* 判断闰年或平年,并进行天数累加 */

if (i % 4 == 0 !(i % 100 == 0) || i % 400 == 0) { // 判断是否为闰年

totalDays = totalDays + 366; // 闰年366天

} else {

totalDays = totalDays + 365; // 平年365天

}

}

/* 计算输入月份之前的天数 */

int beforeDays = 0;

for (int i = 1; i = month; i++) {

switch (i) {

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:

days = 31;

break;

case 2:

if(year % 4 == 0 !(year % 100 == 0) || year % 400 == 0) {

days = 29;

} else {

days = 28;

}

break;

default:

days = 30;

break;

}

if (i month) {

beforeDays = beforeDays + days;

}

}

totalDays = totalDays + beforeDays; // 距离1900年1月1日的天数

/* 计算星期几 */

int firstDayOfMonth; // 存储当月第一天是星期几:星期日为0,星期一~星期六为1~6

int temp = 1 + totalDays % 7; // 从1900年1月1日推算

if (temp == 7) { // 求当月第一天

firstDayOfMonth = 0; // 周日

} else {

firstDayOfMonth = temp;

}

/* 输出日历 */

System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");

for (int nullNo = 0; nullNo firstDayOfMonth; nullNo++) {

System.out.print("\t"); // 输出空格

}

for (int i = 1; i = days; i++) {

System.out.print(i + "\t");

if ((totalDays + i ) % 7 == 6) { // 如果当天为周六,输出换行

System.out.println();

}

}

}

}

java中如何对时间做国际化处理啊

import java.util.*;

import java.text.*;

public class timeText {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

Date now = new Date();

Calendar cal = Calendar.getInstance();

DateFormat d1 = DateFormat.getDateInstance(); //默认语言(汉语)下的默认风格(MEDIUM风格,比如:2008-6-16 20:54:53)

String str1 = d1.format(now);

DateFormat d2 = DateFormat.getDateTimeInstance();//获取系统时间格式

String str2 = d2.format(now); //将时间格式转换成字符串

DateFormat d3 = DateFormat.getTimeInstance();

String str3 = d3.format(now);

DateFormat d4 = DateFormat.getInstance(); //使用SHORT风格显示日期和时间

String str4 = d4.format(now);

DateFormat d5 = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL); //显示日期,周,时间(精确到秒)

String str5 = d5.format(now);

DateFormat d6 = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG); //显示日期。时间(精确到秒)

String str6 = d6.format(now);

DateFormat d7 = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT); //显示日期,时间(精确到分)

String str7 = d7.format(now);

DateFormat d8 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM); //显示日期,时间(精确到分)

String str8 = d8.format(now);//与SHORT风格相比,这种方式最好用

System.out.println("用Date方式显示时间: " + now);//此方法显示的结果和Calendar.getInstance().getTime()一样

System.out.println("用DateFormat.getDateInstance()格式化时间后为:" + str1);

System.out.println("用DateFormat.getDateTimeInstance()格式化时间后为:" + str2);

System.out.println("用DateFormat.getTimeInstance()格式化时间后为:" + str3);

System.out.println("用DateFormat.getInstance()格式化时间后为:" + str4);

System.out.println("用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化时间后为:" + str5);

System.out.println("用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化时间后为:" + str6);

System.out.println("用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化时间后为:" + str7);

System.out.println("用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化时间后为:" + str8);

}

}

运行结果:

用Date方式显示时间: Mon Jun 16 20:54:53 CST 2008

用DateFormat.getDateInstance()格式化时间后为:2008-6-16

用DateFormat.getDateTimeInstance()格式化时间后为:2008-6-16 20:54:53

用DateFormat.getTimeInstance()格式化时间后为:20:54:53

用DateFormat.getInstance()格式化时间后为:08-6-16 下午8:54

用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化时间后为

:2008年6月16日 星期一 下午08时54分53秒 CST

用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化时间后为

:2008年6月16日 下午08时54分53秒

用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化时间后

为:08-6-16 下午8:54

用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化时间

后为:2008-6-16 20:54:53

或者直接获取毫秒,但是感觉与你问题无关


新闻标题:夏令时Java代码处理,java 夏令时
分享网址:http://cdkjz.cn/article/phjdds.html
多年建站经验

多一份参考,总有益处

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

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

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