Java 以 dd-MMM-yyyy 格式获取日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22552530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting date in format dd-MMM-yyyy
提问by user3440926
I have the below method as shown below in which date is coming as of string:
我有以下方法,如下所示,其中日期是字符串:
public static java.sql.Date getSimpleDate11(String dateString) {
if (dateString == null) {
return null;
}
java.util.Date date = null;
java.sql.Date sqlDate =null ;
try {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
df.setLenient(false);
date = df.parse(dateString);
sqlDate = new java.sql.Date(date.getTime());
} catch (ParseException pe) {
throw new RuntimeException("The date entered is invalid or has incorrect format"+ dateString);
}
return sqlDate;
Now I want to change the format: I want to store the date in the format dd-MMM-yyyy.
现在我想更改格式:我想以 dd-MMM-yyyy 格式存储日期。
What should I do?
我该怎么办?
回答by asiya
try this code
试试这个代码
String PATTERN="yyyy-MM-dd";
SimpleDateFormat dateFormat=new SimpleDateFormat();
dateFormat.applyPattern(PATTERN);
String date1=dateFormat.format(Calendar.getInstance().getTime());
回答by Hymanson
java.util.Date.getTime()
method returns the number of milliseconds since January 1, 1970, 00:00:00 GMT
represented by this Date object.
java.util.Date.getTime()
方法返回自此January 1, 1970, 00:00:00 GMT
Date 对象表示以来经过的毫秒数。
public static void main(String[] args) throws ParseException {
String dateString="2014-03-21";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date=df.parse(dateString);
System.out.println("date:"+date);
df=new SimpleDateFormat("dd-MMM-yyyy");
System.out.println("Formated Date:"+df.format(date));
System.out.println("date.getTime"+date.getTime());
}
回答by Ravinder Reddy
now I want to change the format I want to store the date in the format
dd-MMM-yyyy
现在我想更改格式我想以格式存储日期
dd-MMM-yyyy
You don't need an explicit conversion into the requested date format dd-MMM-yyyy
.
Dates are not directly concerned with date formats. Your SQL Driver class will convert to proper database specific format before inserting into a date field of database table.
您不需要显式转换为请求的日期格式dd-MMM-yyyy
。
日期与日期格式没有直接关系。在插入数据库表的日期字段之前,您的 SQL Driver 类将转换为正确的数据库特定格式。
Using MySQL Driver:
使用 MySQL 驱动程序:
// this statement will cause sql date as '2014-03-21'
new java.sql.Date( new java.until.Date().getTime() );
In most of the databases the default format is YYYY-MM-DD
.
在大多数数据库中,默认格式是YYYY-MM-DD
.
Example (MySQL):
示例(MySQL):
mysql> show variables like 'date_format';
+---------------+----------+
| Variable_name | Value |
+---------------+----------+
| date_format | %Y-%m-%d |
+---------------+----------+
1 row in set (0.00 sec)
mysql> select curdate();
+------------+
| curdate() |
+------------+
| 2014-03-21 |
+------------+
1 row in set (0.05 sec)
Detailed Example:
详细示例:
public class SimpleDateFormat_Example {
public static void main(String[] args) throws Exception {
String dateInputPattern = "yyyy-MM-dd"; // numeric 2 digit month
String dateTargetPattern = "yyyy-MMM-dd"; // For 3 char month name
String dateString = "2014-03-20";
patternTest( dateInputPattern, dateString, dateTargetPattern );
System.out.println();
// day of month first and then 2 digit month
dateInputPattern = "yyyy-dd-MM";
dateString = "2014-21-03";
dateTargetPattern = "yyyy-MMMM-dd, EEEE"; // for Full month name
patternTest( dateInputPattern, dateString, dateTargetPattern );
} // psvm( ... )
public static void
patternTest( String dateInputPattern,
String dateString,
String dateTargetPattern ) throws Exception {
java.text.SimpleDateFormat sdf =
new java.text.SimpleDateFormat( dateInputPattern );
java.util.Date date = sdf.parse( dateString );
System.out.println( "Date Pattern: " + dateInputPattern );
System.out.println( "Date String : " + dateString );
System.out.println( "Date Value : " + date );
sdf.applyPattern( dateTargetPattern );
System.out.println( "Target Pattern: " + dateTargetPattern );
System.out.println( "Pattern based Date Value: " + sdf.format(date) );
java.sql.Date sqlDate = new java.sql.Date( date.getTime() );
System.out.println( "But, SQL Date: " + sqlDate );
} // patternTest( s, s, s )
} // end of class SimpleDateFormat_Example
If you run the above program you would be seeing following results.
如果您运行上述程序,您将看到以下结果。
Results:
结果:
Date Pattern: yyyy-MM-dd
Date String : 2014-03-20
Date Value : Thu Mar 20 00:00:00 IST 2014
Target Pattern: yyyy-MMM-dd
Pattern Formatted Date Value: 2014-Mar-20
But, SQL Date: 2014-03-20
Date Pattern: yyyy-dd-MM
Date String : 2014-21-03
Date Value : Fri Mar 21 00:00:00 IST 2014
Target Pattern: yyyy-MMMM-dd, EEEE
Pattern Formatted Date Value: 2014-March-21, Friday
But, SQL Date: 2014-03-21
Have a close look at SQL Date, it has the same pattern in all the outputs. The same is also matching with the result of select curdate()
pattern, as shown in top of this answer.
仔细看看 SQL Date,它在所有输出中都有相同的模式。同样也与select curdate()
模式的结果匹配,如本答案顶部所示。
Conclusion:
结论:
Though you set and apply specific pattern, in your scripting language, for a date type database field, it would only be stored in the default
pattern defined for the database date types.
尽管您在脚本语言中为日期类型数据库字段设置并应用了特定模式,但它只会存储在default
为数据库日期类型定义的模式中。
Hence, converting a pattern from yyyy-MM-dd
to yyyy-MMM-dd
and sending to database will not affect and change anything.
因此,将模式从yyyy-MM-dd
to转换yyyy-MMM-dd
并发送到数据库不会影响和改变任何东西。