java窗口程序字符串时间转成时间戳
pom.xml 文件:
?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>base</artifactId>
<groupId>com.bigdata</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>base-tools</artifactId> <dependencies> <dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency> <dependency>
<groupId>org.eclipse.swt</groupId>
<artifactId>org.eclipse.swt.win32.win32.x86_64</artifactId>
<version>4.3</version>
</dependency> <dependency>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
<version>1.2.17</version>
</dependency> <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
<exclusions>
<exclusion>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
</exclusion>
</exclusions>
</dependency> <dependency>
<groupId>org.eclipse.swt</groupId>
<artifactId>swing2swt</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
</dependencies> <build>
<plugins> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.hikvision.cods.ui.DateFormatMainUI</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin> </plugins>
</build>
</project>
类:
import com.hikvision.cods.util.FontUtils;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.Date;
import java.util.Properties;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.log4j.PropertyConfigurator;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import swing2swt.layout.BorderLayout; public class DateFormatMainUI
{
protected static final Logger logger = LoggerFactory.getLogger(DateFormatMainUI.class);
private static volatile boolean running = true;
public static final Display display = Display.getDefault();
public static final Shell shell = new Shell(192); public static void main(String[] args)
{
Runtime.getRuntime().addShutdownHook(new Thread()
{
public void run()
{
DateFormatMainUI.logger.info("PerfMainUI closed!");
synchronized (DateFormatMainUI.class)
{
DateFormatMainUI.running = false;
DateFormatMainUI.class.notifyAll();
}
}
});
DateFormatMainUI window = new DateFormatMainUI(); window.configLog4j(); window.open();
synchronized (DateFormatMainUI.class)
{
while (running) {
try
{
DateFormatMainUI.class.wait();
}
catch (Throwable localThrowable) {}
}
}
} protected void open()
{
shell.setSize(500, 130);
shell.setText("日期格式转换工具");
shell.setLayout(new BorderLayout(0, 0)); Composite subComposite = new Composite(shell, 0);
subComposite.setLayout(new BorderLayout(5, 5));
loadMainPanel(subComposite); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int width = Math.min(shell.getSize().x, screenSize.width);
int height = Math.min(shell.getSize().y, screenSize.height);
int locX = (screenSize.width - width) / 2;
int locY = (screenSize.height - height) / 2;
shell.setLocation(locX, locY); shell.open();
shell.layout();
shell.addShellListener(new ShellAdapter()
{
public void shellClosed(ShellEvent e)
{
MessageBox messagebox = new MessageBox(DateFormatMainUI.shell, 196);
messagebox.setText("确认");
messagebox.setMessage("您确定要退出吗?");
int message = messagebox.open();
if (((e.doit = message == 64) ? 1 : 0) != 0)
{
DateFormatMainUI.logger.info("++++++++++++++++++++++++++++++++");
System.exit(0);
}
}
});
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
} protected void loadMainPanel(Composite parent)
{
Composite mainCenter = new Composite(parent, 0);
mainCenter.setLayoutData("Center"); GridLayout gl_mainCenter = new GridLayout(1, false);
gl_mainCenter.marginWidth = 0;
gl_mainCenter.marginHeight = 0;
gl_mainCenter.horizontalSpacing = 0;
gl_mainCenter.verticalSpacing = 0;
mainCenter.setLayout(gl_mainCenter); Group detailGroup = new Group(mainCenter, 0);
detailGroup.setLayoutData(new GridData(4, 4, true, true, 1, 1));
detailGroup.setLayout(new GridLayout(5, false)); generateTextLabel(detailGroup, "日期字符: ");
final Text textDateStr = generateText(detailGroup, "", "dateStr");
generateSpliterLabel(detailGroup); generateTextLabel(detailGroup, "日期数值: ");
final Text textDateNum = generateText(detailGroup, "", "dateNum");
generateSpliterLabel(detailGroup); textDateStr.addFocusListener(new FocusListener()
{
public void focusLost(FocusEvent arg0)
{
textDateStr.clearSelection();
Date date = DateFormatUtils.toDate(textDateStr.getText(), "yyyy-MM-dd HH:mm:ss.SSS");
textDateNum.setText(String.valueOf(date == null ? 0L : date.getTime()));
} public void focusGained(FocusEvent arg0)
{
System.out.println("dateStr focusGained..............................");
textDateStr.selectAll();
}
});
textDateStr.addMouseListener(new MouseListener()
{
public void mouseUp(MouseEvent arg0) {} public void mouseDown(MouseEvent arg0) {} public void mouseDoubleClick(MouseEvent arg0)
{
textDateStr.selectAll();
}
});
textDateNum.addFocusListener(new FocusListener()
{
public void focusLost(FocusEvent arg0)
{
textDateNum.clearSelection();
textDateStr.setText(DateFormatUtils.format(NumberUtils.toLong(textDateNum.getText()), "yyyy-MM-dd HH:mm:ss.SSS"));
} public void focusGained(FocusEvent arg0)
{
System.out.println("dateNum focusGained..............................");
textDateNum.selectAll();
}
});
textDateNum.addMouseListener(new MouseListener()
{
public void mouseUp(MouseEvent arg0) {} public void mouseDown(MouseEvent arg0) {} public void mouseDoubleClick(MouseEvent arg0)
{
textDateNum.selectAll();
}
});
} private void convert(Text textDateStr, Text textDateLong)
{
String dateStr = textDateStr.getText();
String dateNum = textDateLong.getText();
if (StringUtils.isEmpty(dateStr))
{
if (StringUtils.isEmpty(dateNum))
{
long timeInMillis = System.currentTimeMillis();
textDateStr.setText(DateFormatUtils.format(timeInMillis, "yyyy-MM-dd HH:mm:ss.SSS"));
textDateLong.setText(String.valueOf(timeInMillis));
}
else
{
long timeInMillis = NumberUtils.toLong(dateNum);
textDateStr.setText(DateFormatUtils.format(timeInMillis, "yyyy-MM-dd HH:mm:ss.SSS"));
}
}
else if (StringUtils.isEmpty(dateNum))
{
Date date = DateFormatUtils.toDate(dateStr, "yyyy-MM-dd HH:mm:ss.SSS");
textDateLong.setText(String.valueOf(date == null ? 0L : date.getTime()));
}
} private void generateSpliterLabel(Group parentGroup)
{
Label label = new Label(parentGroup, 16777224);
label.setLayoutData(new GridData(131072, 16777216, true, false, 1, 1));
} private void generateTextLabel(Group parentGroup, String labelText)
{
Label label = new Label(parentGroup, 16777224);
label.setLayoutData(new GridData(131072, 16777216, true, false, 1, 1));
label.setText(labelText);
label.setFont(FontUtils.getConsolas());
} private Text generateText(Group parentGroup, String defaultText, String name)
{
Text text = new Text(parentGroup, 2048); GridData gridData = new GridData(4, 16777216, true, false, 3, 1);
gridData.widthHint = 280;
gridData.heightHint= 20; text.setLayoutData(gridData);
text.setData("name", name);
text.setText(defaultText);
text.setTextLimit(50);
text.setFont(FontUtils.getConsolas());
return text;
} protected void configLog4j()
{
String level = "DEBUG";
Properties properties = new Properties();
properties.setProperty("log4j.rootLogger", level + ", application");
properties.setProperty("log4j.rootLogger.encoding", "UTF-8");
properties.setProperty("log4j.logger.org.apache", "INFO");
properties.setProperty("log4j.logger.org.springframework", "INFO");
properties.setProperty("log4j.appender.application", "org.apache.log4j.DailyRollingFileAppender");
properties.setProperty("log4j.appender.application.File", "DateFormatMainUI.log");
properties.setProperty("log4j.appender.application.Append", "true");
properties.setProperty("log4j.appender.application.DatePattern", "'.'yyyy-MM-dd");
properties.setProperty("log4j.appender.application.layout", "org.apache.log4j.PatternLayout");
properties.setProperty("log4j.appender.application.layout.ConversionPattern", "%d [%t] %-5p %C{6} (%F:%L) - %m%n");
PropertyConfigurator.configure(properties);
}
}
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.commons.lang.time.FastDateFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; public class DateFormatUtils
{
protected static final Logger LOGGER = LoggerFactory.getLogger(DateFormatUtils.class);
public static final String DATE_TIME_FORMATTER_DEFAULT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
public static final String DATE_TIME_FORMATTER_STANDARD = "yyyy-MM-dd HH:mm:ss";
public static final String DATE_TIME_FORMATTER_STANDARD_TZ = "yyyy-MM-dd'T'HH:mm:ss'Z'";
public static final String DATE_TIME_FORMATTER_LONG = "yyyy-MM-dd HH:mm:ss.SSS";
public static final String DATE_TIME_FORMATTER_LONG_TZ = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
public static final String DATE_TIME_FORMATTER_NET = "yyyy-MM-dd HH:mm:ss.SSS";
public static final long TIMEMILLIS_FOR_ONE_DAY = 86400000L; public static String format(long date)
{
FastDateFormat df = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss'Z'");
return df.format(date);
} public static String format(long date, String format)
{
FastDateFormat df = FastDateFormat.getInstance(format);
return df.format(date);
} public static String format(Date date)
{
FastDateFormat df = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss'Z'");
return df.format(date);
} public static String format(Date date, String format)
{
FastDateFormat df = FastDateFormat.getInstance(format);
return df.format(date);
} public static String format(String dateStr, String srcFormat, String dstFormat)
{
Date date = toDate(dateStr, srcFormat);
FastDateFormat df = FastDateFormat.getInstance(dstFormat);
return df.format(date);
} public static Date toDate(String dateStr, String pattern)
{
try
{
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
return dateFormat.parse(dateStr);
}
catch (ParseException e) {}
return null;
} public static String format___(Date date)
{
FastDateFormat df = FastDateFormat.getInstance("yyyyMMddhh");
return df.format(date);
} public static long getMonthFirstDay(long timeInMillis)
{
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timeInMillis);
calendar.set(5, calendar.getActualMinimum(5));
calendar.set(11, calendar.getActualMinimum(11));
calendar.set(12, calendar.getActualMinimum(12));
calendar.set(13, calendar.getActualMinimum(13));
calendar.set(14, calendar.getActualMinimum(14));
return calendar.getTime().getTime();
} public static long getMonthLastDay(long timeInMillis)
{
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timeInMillis);
calendar.set(5, calendar.getActualMaximum(5));
calendar.set(11, calendar.getActualMaximum(11));
calendar.set(12, calendar.getActualMaximum(12));
calendar.set(13, calendar.getActualMaximum(13));
calendar.set(14, calendar.getActualMaximum(14));
return calendar.getTime().getTime();
} public static long getDateBeginTime(long timeInMillis)
{
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timeInMillis);
calendar.set(11, calendar.getActualMinimum(11));
calendar.set(12, calendar.getActualMinimum(12));
calendar.set(13, calendar.getActualMinimum(13));
calendar.set(14, calendar.getActualMinimum(14));
return calendar.getTime().getTime();
} public static long getDateEndTime(long timeInMillis)
{
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timeInMillis);
calendar.set(11, calendar.getActualMaximum(11));
calendar.set(12, calendar.getActualMaximum(12));
calendar.set(13, calendar.getActualMaximum(13));
calendar.set(14, calendar.getActualMaximum(14));
return calendar.getTime().getTime();
} public static String getNextDay(String dateStr, String format)
{
Date date = toDate(dateStr, format);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(5, calendar.get(5) + 1);
return format(calendar.getTime(), format);
} public static String getLatestSunday(String dateStr, String format)
{
Date date = toDate(dateStr, format);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return format(date.getTime() + (8 - calendar.get(7)) * 86400000L, format);
} public static String getLatestMonthEnd(String dateStr, String format)
{
Date date = toDate(dateStr, format);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(2, calendar.get(2) + 1);
calendar.set(5, calendar.getActualMinimum(5));
return format(calendar.getTime(), format);
} public static void main(String[] args)
{
System.out.println(getNextDay("150205", "yyMMdd"));
System.out.println(getLatestSunday("150205", "yyMMdd"));
System.out.println(getLatestMonthEnd("150205", "yyMMdd")); System.out.println(getNextDay("150208", "yyMMdd"));
System.out.println(getLatestSunday("150209", "yyMMdd"));
System.out.println(getLatestMonthEnd("150209", "yyMMdd"));
}
}
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.widgets.Display; public class FontUtils
{
static final int FONT_HIGHT_DEFAULT = 12;
static final int FONT_HIGHT_BANNER = 30;
static Font bannerFont = new Font(Display.getCurrent(), "黑体", 30, 1);
static Font consolas = new Font(Display.getCurrent(), "Consolas", 12, 0); public static Font getBanner()
{
return bannerFont;
} public static Font getConsolas()
{
return consolas;
}
}
java窗口程序字符串时间转成时间戳的更多相关文章
- 字符串时间与Unix时间戳相互转换
字符串时间与Unix时间戳相互转换 /** * @Author: wangkun * @Date : 2016/1/21 13:43 * @Description : 字符串时间转换为Unix时间戳 ...
- 使用Java窗口程序执行输入的任何cmd命令
利用Java窗口程序来执行用输入的任何命令 实现效果: Java桌面窗口,输入框.按钮,当输入框被输入命令的时候,点击按钮执行命令! 实现代码 package com.remote.remote.ag ...
- vue ele 日期时间格式限制不能早于当天,时间转换成时间戳 进行比较
<el-date-picker value-format="yyyy-MM-dd HH:mm:ss" v-model=&quo ...
- JAVA将数字字符串强制转换成整型变量----求参数之和实验代码(附流程图)
一.设计思想 先将参数个数输出,并利用循环结果将参数逐个输出,再将字符串强制转化成整型,利用循环结构相加求和 二.程序流程图 三.源程序代码 package demo; public class Co ...
- 安卓、ios时间转换成时间戳的形式
将日期转换成时间戳的形式,在安卓和ios不同的系统下转正会有兼容性的问题 安卓系统下Date.parse(new Date('2018-03-30 12:00:00'))会直接转换成时间戳的形式(简单 ...
- JAVA窗口程序实例一
package 甲; import java.awt.Dimension; import java.text.SimpleDateFormat; import java.util.Calendar; ...
- js 时间转成时间戳对比;My97DatePicker日历控件时间格式;Date.parse Firefox火狐浏览器返回Nan的解决办法
有个情况,我在显示时间的时候是需要显示为 2013年8月15日 14时28分15秒 但是假如我用js去获取到这个时间,并且想进行时间对比的时候,这个时间2013年8月15日 14时28分15秒根本就 ...
- 微信小程序中时间转化为时间戳(安卓和苹果兼容性)
在IOS中时间显示NAN,后来才知道是由于安卓和IOS时间解析时间的时间格式不一致: 在安卓机中2018-06-21 16:00:21 IOS识别的格式是 2018/06/21: 所以在处理IOS机器 ...
- js将格式化的时间转换成时间戳
var timestamp1 = Date.parse(new Date(startTime)), timestamp2 = Date.parse(new Date(endTime));;consol ...
随机推荐
- 我的reshape观
reshape(1,2)把结果分成1块,每一块2个元素 reshape(2,1)把结果分成2块,每一块1个元素 reshape(-1,1)把结果分成任意块,每一块1个元素 reshape(1,-1)把 ...
- IntelliJ IDEA 2017.3尚硅谷-----设置自动编译
- 虚拟机下的Linux连接校园网上网问题
安装了CentOS7后,本地Windows系统连接用的是校园网,虚拟机用的是桥接模式,无法上网. 解决的办法是:找到连接的校园网的网卡,给虚拟机的VMnet8共享. 步骤:打开[网络和共享中心]-[更 ...
- python面试的100题(1)
题目:有一个jsonline格式的文件file.txt大小约为10K def get_lines(): with open('file.txt','rb') as f: return f.readli ...
- Anaconda的安装及tensorflow和各个库的安装
首先,在anaconda官网https://www.anaconda.com/download/下载想要的版本,2.7或者3+,建议用3.0以上的版本,因为相对来说,功能更加的多样. 下载完成后将安装 ...
- navicat导入.csv表格
我本地的navicat不知道啥情况,导入不了表格,然后把表格转为.csv的,然后导入就好了 1.表格另存为.csv格式的 2.打开Navicat,选择要导入的表,然后右键->导入向导,选择.cs ...
- 吴裕雄 python 机器学习——半监督学习LabelSpreading模型
import numpy as np import matplotlib.pyplot as plt from sklearn import metrics from sklearn import d ...
- idea中的springboot+gradle项目报错springboot configuration annotation processor not found in classpath
idea中的springboot项目,打开某个类run.halo.app.config.properties.HaloProperties.java,报错(使用gradle编译): springboo ...
- +(new Date())
+(new Date()) 等于 new Date().getTime();展示 1561003191879 毫秒时间戳
- composer update 或者 composer install提示killed解决办法
出现此原因大多因为缓存不足造成,在linux环境可增加缓存解决. free -mmkdir -p /var/_swap_cd /var/_swap_#Here, 1M * 2000 ~= 2GB of ...