public static final int DATE = 1;
public static final int TIME = 2;
public static final int DATE_TIME = 3;
private static void dd(StringBuffer buf, int i) {
buf.append((char) (((int) '0') + i / 10));
buf.append((char) (((int) '0') + i % 10));
}
public static String dateToString(Date date, int type) {
Calendar c = Calendar.getInstance();
c.setTimeZone(TimeZone.getTimeZone("GMT"));
c.setTime(date);
StringBuffer buf = new StringBuffer();
if ((type & DATE) != 0) {
int year = c.get(Calendar.YEAR);
dd(buf, year / 100);
dd(buf, year % 100);
buf.append('-');
dd(buf, c.get(Calendar.MONTH) - Calendar.JANUARY + 1);
buf.append('-');
dd(buf, c.get(Calendar.DAY_OF_MONTH));
if (type == DATE_TIME)
buf.append("T");
}
if ((type & TIME) != 0) {
dd(buf, c.get(Calendar.HOUR_OF_DAY));
buf.append(':');
dd(buf, c.get(Calendar.MINUTE));
buf.append(':');
dd(buf, c.get(Calendar.SECOND));
buf.append('.');
int ms = c.get(Calendar.MILLISECOND);
buf.append((char) (((int) '0') + (ms / 100)));
dd(buf, ms % 100);
buf.append('Z');
}
return buf.toString();
}
public static Date stringToDate(String text, int type) {
Calendar c = Calendar.getInstance();
if (type != DATE_TIME)
c.setTime(new Date(0));
if ((type & DATE) != 0) {
c.set(Calendar.YEAR, Integer.parseInt(text.substring(0, 4)));
c.set(Calendar.MONTH, Integer.parseInt(text.substring(5, 7)) - 1
+ Calendar.JANUARY);
c.set(Calendar.DAY_OF_MONTH,
Integer.parseInt(text.substring(8, 10)));
if (type == DATE_TIME)
text = text.substring(11);
}
if ((type & TIME) == 0)
return c.getTime();
c.set(Calendar.HOUR_OF_DAY, Integer.parseInt(text.substring(0, 2))); // -11
c.set(Calendar.MINUTE, Integer.parseInt(text.substring(3, 5)));
c.set(Calendar.SECOND, Integer.parseInt(text.substring(6,
));
int pos = 8;
if (pos < text.length() && text.charAt(pos) == '.') {
int ms = 0;
int f = 100;
while (true) {
char d = text.charAt(++pos);
if (d < '0' || d > '9')
break;
ms += (d - '0') * f;
f /= 10;
}
c.set(Calendar.MILLISECOND, ms);
} else
c.set(Calendar.MILLISECOND, 0);
if (pos < text.length()) {
if (text.charAt(pos) == '+' || text.charAt(pos) == '-')
c.setTimeZone(TimeZone.getTimeZone("GMT" + text.substring(pos)));
/*
* return new Date (c.getTime ().getTime () + (Integer.parseInt
* (text.substring (pos+1, pos+3)) * 60 + Integer.parseInt
* (text.substring (pos+4, pos+6))) (text.charAt (pos) == '-' ?
* -60000 : 60000));
*/
else if (text.charAt(pos) == 'Z')
c.setTimeZone(TimeZone.getTimeZone("GMT"));
else
throw new RuntimeException("illegal time format!");
}
return c.getTime();
}
/**
* 取得当月天数
* */
public static int getCurrentMonthLastDay()
{
Calendar a = Calendar.getInstance();
a.set(Calendar.DATE, 1);//把日期设置为当月第一天
a.roll(Calendar.DATE, -1);//日期回滚一天,也就是最后一天
int maxDate = a.get(Calendar.DATE);
return maxDate;
}
/**
* 得到指定月的天数
* */
public static int getMonthLastDay(int year, int month)
{
Calendar a = Calendar.getInstance();
a.set(Calendar.YEAR, year);
a.set(Calendar.MONTH, month - 1);
a.set(Calendar.DATE, 1);//把日期设置为当月第一天
a.roll(Calendar.DATE, -1);//日期回滚一天,也就是最后一天
int maxDate = a.get(Calendar.DATE);
return maxDate;
}
//返回当前年份
public static int getYear()
{
Date date = new Date();
String year = new SimpleDateFormat("yyyy").format(date);
return Integer.parseInt(year);
}
//返回当前月份
public static int getMonth()
{
Date date = new Date();
String month = new SimpleDateFormat("MM").format(date);
return Integer.parseInt(month);
}
分享到:
相关推荐
utc秒数时间与日期时间互相转换的工具,注意是相互,不是单向。有时候有点用。
自己写的小工具,提供utc秒数时间与日期时间之间的互相转换。
约会时间漂亮的日期时间: 2014-01-09 06:46:01安装$ npm install date-time用法import dateTime from 'date-time' ;dateTime ( ) ;//=> '2017-05-20 17:07:05'dateTime ( { date : new Date ( 1989 , 2 , 4 , 10 ) ...
易语言取UTC时间源码,取UTC时间,WSAStartup,socket,getpeername,Connect,GetDateFormat,GetLocalTime,GetTimeFormat,recv,closesocket,WSACleanup
%UTC2GPS 将 UTC(GMT) 时间标签转换为 GPS 时间占闰秒% UTC2GPS(date) 校正 UTC 日期数组(任何 matlab 格式) % 闰秒并返回 GPS 日期数组,其中: % GPS = UTC + 步进时间% 当前步骤时间是到 2009 年 1 月 1 日,但...
%GPS2UTC 将 GPS 时间标签转换为 UTC(GMT) 时间,考虑闰秒% GPS2UTC(date) 更正 GPS 日期数组(任何 matlab 格式) % 闰秒并返回一个 UTC 日期数组,其中: % UTC = GPS - 步进时间% 当前步骤时间是到 2009 年 1 月 ...
自己写的小工具,提供utc秒数时间与日期时间之间的互相转换。 此版本为2.0 for MAC OS X 版本,也是该程序在MAC平台的首个版本。 2.0版本主要改进如下: 1.增加日历功能,方便日期选择 2.输出结果支持右键菜单...
UTC日期 仅适用于固定 UTC 偏移量的 JavaScript 日期替换。 免责声明 这是在 alpha 阶段,还没有准备好生产。 它可能与另一种工具结合使用来检测发生在 Date 对象上的 DST 变化,因此可以实现一个半独立的 Date,该 ...
自己写的小工具,提供utc秒数时间与日期时间之间的互相转换。 此版本为2.0版本,主要改进如下: 1.增加日历功能,方便日期选择 2.输出结果支持右键菜单复制、粘贴 本程序需要VC 2008运行库支持,如果无法执行,请...
杰安德鲁 处理服务器UTC时间并相应地进行转换以满足您的android需求
GPS UTC与北京时间的转换函数,方便使用。
<9> UTC日期,ddmmyy(日月年)格式 <10> 磁偏角(000.0~180.0度,前面的0也将被传输) <11> 磁偏角方向,E(东)或W(西) <12> 模式指示(仅NMEA0183 3.00版本输出,A=自主定位,D=差分,E=估算,N=数据无效)
今天将要介绍的是vue中的过滤器,并且将实现一个日期格式化的小案例。 大家都知道,我们获取当前日期可以通过Date对象获取。下面我将获取当前时间并打印出来。 console.log(new Date()); 我们获取的是一个标准时间...
主要介绍了JavaScript UTC时间转换方法,介绍了本地时间到UTC时间的转换、UTC日期到本地日期的转换,感兴趣的小伙伴们可以参考一下
3.2打印出UTC日期和时间 3.3打印出一个ISO 8601格式日期 3.4把一个ISO 8601格式的日期转换为Date对象可接受的一种格式 3.5创建一个特定的日期 3.6规划未来的一个日期 3.7记录流逝的时间 3.8创建一个...
角本地日期 AngularJS的本地日期支持 动机 ... “本地日期”的一个示例是出生日期,该... 解决方法是在输入标签上使用ng-model-options="{timezone: 'UTC'}"以保证UTC日期时间。 将Date序列化为JSON将始终产生完整的ISO-8
能够将UTC标准时间转化成为GPS标准时间(包括GPS周数和周内秒数)。(UTC standard time can be transformed into GPS standard time (weeks and weeks, including GPS seconds).)
TDateTime转UTC的时间差.mht
实现UTC与日期时间的互相转换,在网上看到的,觉得挺好用的
显示精确的本地和utc日期和时间 确切时间显示您当地时区的精确日期和时间,以及当前的UTC(世界标准时间)。只需单击Chrome工具栏上的“确切时间”图标,即可查看显示本地和UTC日期和时间的运行时钟。无需其他配置。...