Abh*_*hra 71 java date simpledateformat
任何人都可以告诉我SimpleDateFormat类中可用的日期格式.
我已经通过api但找不到满意的答案.非常感谢任何帮助.
Sub*_*der 108
日期和时间格式在下面有详细描述
SimpleDateFormat(Java Platform SE 7) - 日期和时间模式
有可能是n格式,你可以做可能数.ex - dd/MM/yyyy或者YYYY-'W'ww-u你可以混合和匹配字母以达到你所需的模式.图案字母如下.
G - 时代指示符(AD)y - 年份(1996年; 96年)Y - 周年(2009年; 09年)M - 逐年(7月; 7月; 07年)w - 一年中的一周(27)W - 每月一周(2)D - 一年中的一天(189)d - 每月的日子(10)F - 每月的某一天(2)E - 星期的名字(星期二;星期二)u - 星期数(1 =星期一,...,7 =星期日)a - AM/PM标记H - 一天中的小时(0-23)k - 一天中的小时(1-24)K - 上午/下午(0-11)h - 上午/下午(1-12)m - 分钟(30)s - 分钟秒(55)S - 毫秒(978)z - 一般时区(太平洋标准时间;太平洋标准时间;格林威治标准时间-08:00)Z - RFC 822时区(-0800)X - ISO 8601时区(-08; -0800; -08:00)Roy*_*Roy 51
让我抛出一些我从http://www3.ntu.edu.sg/home/ehchua/programming/java/DateTimeCalendar.html获得的示例代码 然后你可以使用不同的选项,直到你理解它为止.
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTest {
public static void main(String[] args) {
Date now = new Date();
//This is just Date's toString method and doesn't involve SimpleDateFormat
System.out.println("toString(): " + now); // dow mon dd hh:mm:ss zzz yyyy
//Shows "Mon Oct 08 08:17:06 EDT 2012"
SimpleDateFormat dateFormatter = new SimpleDateFormat("E, y-M-d 'at' h:m:s a z");
System.out.println("Format 1: " + dateFormatter.format(now));
// Shows "Mon, 2012-10-8 at 8:17:6 AM EDT"
dateFormatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
System.out.println("Format 2: " + dateFormatter.format(now));
// Shows "Mon 2012.10.08 at 08:17:06 AM EDT"
dateFormatter = new SimpleDateFormat("EEEE, MMMM d, yyyy");
System.out.println("Format 3: " + dateFormatter.format(now));
// Shows "Monday, October 8, 2012"
// SimpleDateFormat can be used to control the date/time display format:
// E (day of week): 3E or fewer (in text xxx), >3E (in full text)
// M (month): M (in number), MM (in number with leading zero)
// 3M: (in text xxx), >3M: (in full text full)
// h (hour): h, hh (with leading zero)
// m (minute)
// s (second)
// a (AM/PM)
// H (hour in 0 to 23)
// z (time zone)
// (there may be more listed under the API - I didn't check)
}
Run Code Online (Sandbox Code Playgroud)
}
祝好运!
更新
其他问题已经过时了。SimpleDateFormat几年前被现代java.time类取代的可怕的遗留类。
为了定义您自己的自定义格式模式,中的代码与DateTimeFormatter中的代码类似但不完全相同SimpleDateFormat。一定要研究文档。并在 Stack Overflow 中搜索许多示例。
DateTimeFormatter f =
DateTimeFormatter.ofPattern(
"dd MMM uuuu" ,
Locale.ITALY
)
;
Run Code Online (Sandbox Code Playgroud)
在ISO 8601标准定义格式的多种类型的日期时间值的。这些格式是为数据交换而设计的,可以很容易地被机器解析,也很容易被跨文化的人类阅读。
该java.time类生成/解析字符串时,使用ISO 8601种格式默认。只需调用toString&parse方法。无需指定格式模式。
Instant.now().toString()
Run Code Online (Sandbox Code Playgroud)
2018-11-05T18:19:33.017554Z
对于 UTC 中的值,Z末尾的 表示 UTC,发音为“Zulu”。
您可以让java.time 自动为您本地化,而不是指定格式模式。使用DateTimeFormatter.ofLocalized…方法。
使用特定地区(时区)的人们使用的挂钟时间获取当前时刻。
ZoneId z = ZoneId.of( "Africa/Tunis" );
ZonedDateTime zdt = ZonedDateTime.now( z );
Run Code Online (Sandbox Code Playgroud)
以标准 ISO 8601 格式生成文本,明智地扩展为在方括号中附加时区名称。
zdt.toString(): 2018-11-05T19:20:23.765293+01:00[非洲/突尼斯]
生成自动本地化的文本。
Locale locale = Locale.CANADA_FRENCH;
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL ).withLocale( locale );
String output = zdt.format( f );
Run Code Online (Sandbox Code Playgroud)
输出:lundi 2018 年 11 月 5 日 à 19:20:23 heure normale d'Europe centrale
通常是自动本地化而不是使用硬编码格式模式的更好做法。
该java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date, Calendar, & SimpleDateFormat。
要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310。
现在处于维护模式的Joda-Time项目建议迁移到java.time类。
您可以直接与您的数据库交换java.time对象。使用符合JDBC 4.2或更高版本的JDBC 驱动程序。不需要字符串,不需要类。Hibernate 5 & JPA 2.2 支持java.time。java.sql.*
从哪里获得 java.time 类?
检查这里的格式 http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
主要
System.out.println("date : " + new classname().getMyDate("2014-01-09 14:06", "dd-MMM-yyyy E hh:mm a z", "yyyy-MM-dd HH:mm"));
Run Code Online (Sandbox Code Playgroud)
方法
public String getMyDate(String myDate, String returnFormat, String myFormat)
{
DateFormat dateFormat = new SimpleDateFormat(returnFormat);
Date date=null;
String returnValue="";
try {
date = new SimpleDateFormat(myFormat, Locale.ENGLISH).parse(myDate);
returnValue = dateFormat.format(date);
} catch (ParseException e) {
returnValue= myDate;
System.out.println("failed");
e.printStackTrace();
}
return returnValue;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
237448 次 |
| 最近记录: |