日期格式轉換
日期格式轉換| Id | Title | DateAdded | SourceUrl | PostType | Body | BlogId | Description | DateUpdated | IsMarkdown | EntryName | CreatedTime | IsActive | AutoDesc | AccessPermission |
| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------|
| 16938008| 日期格式轉換| 2022-11-30T11:56:00| | BlogPost|
import java.util.Date;
import java.text.DateFormat;
/**
* 格式化时间类
* DateFormat.FULL = 0
* DateFormat.DEFAULT = 2
* DateFormat.LONG = 1
* DateFormat.MEDIUM = 2
* DateFormat.SHORT = 3
* @author Michael
* @version 1.0, 2007/03/09
*/
public class Test{
public static void main(String []args){
Date d = new Date();
String s;
/** Date类的格式: Sat Apr 16 13:17:29 CST 2006 */
System.out.println(d);
System.out.println("******************************************");
/** getDateInstance() */
/** 输出格式: 2006-4-16 */
s = DateFormat.getDateInstance().format(d);
System.out.println(s);
/** 输出格式: 2006-4-16 */
s = DateFormat.getDateInstance(DateFormat.DEFAULT).format(d);
System.out.println(s);
/** 输出格式: 2006年4月16日 星期六 */
s = DateFormat.getDateInstance(DateFormat.FULL).format(d);
System.out.println(s);
/** 输出格式: 2006-4-16 */
s = DateFormat.getDateInstance(DateFormat.MEDIUM).format(d);
System.out.println(s);
/** 输出格式: 06-4-16 */
s = DateFormat.getDateInstance(DateFormat.SHORT).format(d);
System.out.println(s);
/** 输出格式: 2006-01-01 00:00:00 */
java.text.DateFormat format1 = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
s = format1.format(new Date());
System.out.println(s);
/** 输出格式: 2006-01-01 01:00:00 */
System.out.println((new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss")).format(new Date()));
/** 输出格式: 2006-01-01 13:00:00 */
System.out.println((new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date()));
/** 输出格式: 20060101000000***/
java.text.DateFormat format2 = new java.text.SimpleDateFormat("yyyyMMddhhmmss");
s = format2.format(new Date());
System.out.println(s);
}
}
补充一下:
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
System.out.println("System Date: " + cal.get(Calendar.MONTH+1));
注意下,月份是从0开始的,要Calendar.MONTH+1才可以的
| 648658| | 2022-11-30T11:56:00| false| | 2022-11-30T11:55:43.203| true| import java.util.Date; import java.text.DateFormat; /** * 格式化时间类 * DateFormat.FULL = 0 * DateFormat.DEFAULT = 2 * DateFormat.LONG = 1 * DateFormat.MED| Anonymous|
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现