dateformat(请教关于linux下java类DateFormat的使用)

本文目录
- 请教关于linux下java类DateFormat的使用
- format可以用在SQL语句中吗怎么用,谢谢
- Java中SimpleDateFormate是线程不安全的又会怎样
- dateformat()怎么用
- DateFormat的使用方法
- SimpleDateFormat parse format区别
请教关于linux下java类DateFormat的使用
1:DateDemo1类
[java] view plain copy
/*
* 取得系统的时间
*/
public class DateDemo1 {
public static void main(String args) {
System.out.println(System.currentTimeMillis());
}
输出为:1311060393171
执行结果会显示从1970年1月1日开始到取得系统时间为止所经过的毫秒数,例如1115346430703这个数字,但这样的数字没有人确切了解它的意 义是什么,您可以使用Date类别来让这个数字变的更有意义一些
2:DateDemo2类
[java] view plain copy
public class DateDemo2 {
@SuppressWarnings(“deprecation“)
public static void main(String args) {
Date date = new Date();
System.out.println(“date.toString: “ + date.toString());
System.out.println(“date: “ + date.getDate());
System.out.println(“day: “ + date.getDay());
System.out.println(“hour: “ + date.getHours());
System.out.println(“minutes: “ + date.getMinutes());
System.out.println(“month: “ + (date.getMonth() + 1));
System.out.println(“seconds: “ + date.getSeconds());
System.out.println(“time: “ + date.getTime());
System.out.println(“timezone: “ + date.getTimezoneOffset());
System.out.println(“year: “ + (date.getYear() + 1900));
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DATE);
int hour = calendar.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
System.out.println(“----------Calendar-------“);
System.out.println(“year: “ + year);
System.out.println(“month: “ + month);
System.out.println(“day: “ + day);
System.out.println(“hour: “ + hour);
System.out.println(“minute: “ + minute);
System.out.println(“second: “ + second);
}
}
用date获得时间的值,如时、分、秒、月、日、年得方法已经过时,需要改用Calendar的方法来获取时间的值
输出结果为:
[java] view plain copy
date.toString: Tue Jul 19 15:32:42 CST 2011
date: 19
day: 2
hour: 15
minutes: 32
month: 7
seconds: 42
time: 1311060762362
timezone: -480
year: 2011
----------Calendar-------
year: 2011
month: 7
day: 19
hour: 3
minute: 32
second: 42
3:DateDemo3类
[java] view plain copy
public class DateDemo3 {
public static void main(String args) {
Date date = new Date();
/*
* DateFormat会依电脑上的区域设定显示时间格式,EE表示星期,MM表示月份、dd表示日期,而yyyy是西元,每个字元的设定都各有其意义
* 从Date--》String
*/
DateFormat dateFormat = new SimpleDateFormat(“EE-MM-dd-yyyy“);
System.out.println(dateFormat.format(date));
DateFormat format1 = new SimpleDateFormat(“yyyy-MM-dd“);
System.out.println(format1.format(date));
DateFormat format2 = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss“);
System.out.println(format2.format(date));
/*
* 从String--》Date
*/
String dateStr = “2011-7-19“;
try {
Date dateTrans = format1.parse(dateStr);
//System.out.println(dateTrans.toString());
System.out.println(dateTrans.toLocaleString()); //转换为本地的形式
} catch (ParseException e) {
e.printStackTrace();
}
}
}
使用DateFormat来格式化日期数据,上面部分为从Date--》String,下面的部分为从String--》Date
输出结果为:
[java] view plain copy
星期二-07-19-2011
2011-07-19
2011-07-19 15:33:27
2011-7-19 0:00:00
4:DateDemo4类
[java] view plain copy
public class DateDemo4 {
public static void main(String args) {
Date date = new Date();
DateFormat shortFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
DateFormat mediumFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
DateFormat longFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
DateFormat fullFormat = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
System.out.println(shortFormat.format(date));
System.out.println(mediumFormat.format(date));
System.out.println(longFormat.format(date));
System.out.println(fullFormat.format(date));
}
}
输出结果为:
[java] view plain copy
11-7-19 下午3:33
2011-7-19 15:33:54
2011年7月19日 下午03时33分54秒
2011年7月19日 星期二 下午03时33分54秒 CST
5:DateDemo5类
[java] view plain copy
public class DateDemo5 {
public static void main(String args) {
Date date = new Date();
Locale locale = new Locale(“en“, “US“);
DateFormat shortDateFormat = DateFormat.getDateInstance(DateFormat.SHORT, locale);
DateFormat mediumDateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
DateFormat longDateFormat = DateFormat.getDateInstance(DateFormat.LONG, locale);
DateFormat fullDateFormat = DateFormat.getDateInstance(DateFormat.FULL, locale);
System.out.println(shortDateFormat.format(date));
System.out.println(mediumDateFormat.format(date));
System.out.println(longDateFormat.format(date));
System.out.println(fullDateFormat.format(date));
}
}
输出结果为:
[java] view plain copy
7/19/11
Jul 19, 2011
July 19, 2011
Tuesday, July 19, 2011
format可以用在SQL语句中吗怎么用,谢谢
FormatDateTime(Date[, NamedFormat])
参数
Date
必选项。要被格式化的日期表达式。
NamedFormat
可选项。指示所使用的日期/时间格式的数值,如果省略,则使用 vbGeneralDate。
设置
NamedFormat 参数可以有以下值:
常数 值 描述
vbGeneralDate 0 显示日期和/或时间。如果有日期部分,则将该部分显示为短日期格式。如果有时间部分,则将该部分显示为长时间格
式。如果都存在,则显示所有部分。
vbLongDate 1 使用计算机区域设置中指定的长日期格式显示日期。
vbShortDate 2 使用计算机区域设置中指定的短日期格式显示日期。
vbLongTime 3 使用计算机区域设置中指定的时间格式显示时间。
vbShortTime 4 使用 24 小时格式 (hh:mm) 显示时间。
说明
下面例子利用 FormatDateTime 函数把表达式格式化为长日期型并且把它赋给 MyDateTime:
Function GetCurrentDate
’FormatDateTime 把日期型格式化为长日期型。
GetCurrentDate = FormatDateTime(Date, 1)
End Function
Java中SimpleDateFormate是线程不安全的又会怎样
如果你的代码所在的进程中有多个线程在同时运行,而这些线程可能会同时运行这段代码。如果每次运行结果和单线程运行的结果是一样的,而且其他的变量的值也和预期的是一样的,就是线程安全的。
或者说:一个类或者程序所提供的接口对于线程来说是原子操作或者多个线程之间的切换不会导致该接口的执行结果存在二义性,也就是说我们不用考虑同步的问题。
线程安全问题都是由全局变量及静态变量引起的。
若每个线程中对全局变量、静态变量只有读操作,而无写操作,一般来说,这个全局变量是线程安全的;若有多个线程同时执行写操作,一般都需要考虑线程同步,否则就可能影响线程安全。
dateformat()怎么用
dateformat()SQL 设置时间格式
getdate() 今天
getdate()-1就是昨天 dateadd(mm, -12,getdate()-1) 昨天减12个月 就是去年的昨天
dateformat(A,B) A是要设置的时间 B是格式 比如’yyyy-MM’就是 2008-04这个的格式
DateFormat的使用方法
一、在oracle中,当想把字符串为‘2011-09-20 08:30:45’的格式转化为日期格式,我们可以使用oracle提供的to_date函数。
sql语句为:
SELECT to_date(’2011-09-20 08:30:45’, ’yyyy-MM-dd hh24:mi:ss’) FROM dual 反之,可以使用to_char()函数把日期转化为字符串。
sql语句为:
SELECT to_char(SYSDATE, ’yyyy-MM-dd hh24:mi:ss’) FROM dual;
二、在mysql中,DATE_FORMAT(date, format) 函数根据format字符串格式化date值。
%M 月名字(January……December)
%W 星期名字(Sunday……Saturday)
%D 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。)
%Y 年, 数字, 4 位
%y 年, 数字, 2 位
%a 缩写的星期名字(Sun……Sat)
%d 月份中的天数, 数字(00……31)
%e 月份中的天数, 数字(0……31)
%m 月, 数字(01……12)
%c 月, 数字(1……12)
%b 缩写的月份名字(Jan……Dec)
%j 一年中的天数(001……366)
%H 小时(00……23)
%k 小时(0……23)
%h 小时(01……12)
%I 小时(01……12)
%l 小时(1……12)
%i 分钟, 数字(00……59)
%r 时间,12 小时(hh:mm:ss [AP]M)
%T 时间,24 小时(hh:mm:ss)
%S 秒(00……59)
%s 秒(00……59)
%p AM或PM
%w 一个星期中的天数(0=Sunday ……6=Saturday )
%U 星期(0……52), 这里星期天是星期的第一天
%u 星期(0……52), 这里星期一是星期的第一天
%% 一个文字“%”。
把字符串转为日期格式
SELECT DATE_FORMAT(’2011-09-20 08:30:45’, ’%Y-%m-%d %H:%i:%S’);
把日期转为字符串格式
SELECT DATE_FORMAT(NOW(), ’%Y-%m-%d %H:%i:%S’);
SimpleDateFormat parse format区别
Format方法有两种形式,另外一种是三个参数的,主要区别在于它是线程安全的,
但并不多用,所以这里只对第一个介绍。Format参数是一个格式字符串,用于格式化Args里面的值的。Args是一个变量数组,即它里面可以有多个参数,而且每个参数可以不同。
例如:
Format(“my
name
is
%6s“,“wind“);
返回的是:my
name
is
wind
Format里面可以写普通的字符串,比如“my
name
is“
,但有些格式指令字符具有特殊意义,比如“%6s“
格式指令具有以下的形式:
“%“
[index
“:“]
[“-“]
[width]
[“.“
prec]
type
它是以“%“开始,而以type结束,type表示一个具体的类型。中间是用来格式化type类型的指令字符,是可选的。

更多文章:
repercussions(都是余波,repercussions和aftermath有什么区别啊)
2026年10月11日 09:00
dropdownlist 绑定(DropDownList 绑定所有项 并 显示指定项)
2026年10月11日 08:50
易语言网页api接口怎么调用(易语言,怎么读取网页json的api)
2026年10月11日 08:00
majority of(the majority of 和 a majority of的区别以及用法例句)
2026年10月11日 07:40
another time(another time和other time的区别)
2026年10月11日 05:00




