Java时间格式转换大全

59 篇文章 0 订阅

import java.text.*;
import java.util.Calendar;
public class VeDate {
/**
 * 获取现在时间
 *
* @return 返回时间类型 yyyy-MM-dd HH:mm:ss
 */
public static Date getNowDate() {
 Date currentTime = new Date();
 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 String dateString = formatter.format(currentTime);
 ParsePosition pos = new ParsePosition(8);
 Date currentTime_2 = formatter.parse(dateString, pos);
 return currentTime_2;
}
/**
 * 获取现在时间
 *
* @return返回短时间格式 yyyy-MM-dd
 */
DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
DateFormat format 2= new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
Date date = null;
String str = null;

// String转Date
str = "2007-1-18";
try {
date = format1.parse(str);
data = format2.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
/**
 * 获取现在时间
 *
* @return返回字符串格式 yyyy-MM-dd HH:mm:ss
 */
public static String getStringDate() {
 Date currentTime = new Date();
 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 String dateString = formatter.format(currentTime);
 return dateString;
}
/**
 * 获取现在时间
 *
* @return 返回短时间字符串格式yyyy-MM-dd
 */
public static String getStringDateShort() {
 Date currentTime = new Date();
 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
 String dateString = formatter.format(currentTime);
 return dateString;
}
/**
 * 获取时间 小时:分;秒 HH:mm:ss
 *
* @return
 */
public static String getTimeShort() {
 SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
 Date currentTime = new Date();
 String dateString = formatter.format(currentTime);
 return dateString;
}
/**
 * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
 *
* @param strDate
 * @return
 */
public static Date strToDateLong(String strDate) {
 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 ParsePosition pos = new ParsePosition(0);
 Date strtodate = formatter.parse(strDate, pos);
 return strtodate;
}
/**
 * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
 *
* @param dateDate
 * @return
 */
public static String dateToStrLong(java.util.Date dateDate) {
 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 String dateString = formatter.format(dateDate);
 return dateString;
}
/**
 * 将短时间格式时间转换为字符串 yyyy-MM-dd
 *
* @param dateDate
 * @param k
 * @return
 */
public static String dateToStr(java.util.Date dateDate) {
 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
 String dateString = formatter.format(dateDate);
 return dateString;
}
/**
 * 将短时间格式字符串转换为时间 yyyy-MM-dd
*
* @param strDate
 * @return
 */
public static Date strToDate(String strDate) {
 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
 ParsePosition pos = new ParsePosition(0);
 Date strtodate = formatter.parse(strDate, pos);
 return strtodate;
}
/**
 * 得到现在时间
 *
* @return
 */
public static Date getNow() {
 Date currentTime = new Date();
 return currentTime;
}
/**
 * 提取一个月中的最后一天
 *
* @param day
 * @return
 */
public static Date getLastDate(long day) {
 Date date = new Date();
 long date_3_hm = date.getTime() - 3600000 * 34 * day;
 Date date_3_hm_date = new Date(date_3_hm);
 return date_3_hm_date;
}
/**
 * 得到现在时间
 *
* @return 字符串 yyyyMMdd HHmmss
 */
public static String getStringToday() {
 Date currentTime = new Date();
 SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");
 String dateString = formatter.format(currentTime);
 return dateString;
}
/**
 * 得到现在小时
 */
public static String getHour() {
 Date currentTime = new Date();
 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 String dateString = formatter.format(currentTime);
 String hour;
 hour = dateString.substring(11, 13);
 return hour;
}
/**
 * 得到现在分钟
 *
* @return
 */
public static String getTime() {
 Date currentTime = new Date();
 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 String dateString = formatter.format(currentTime);
 String min;
 min = dateString.substring(14, 16);
 return min;
}
/**
 * 根据用户传入的时间表示格式,返回当前时间的格式 如果是yyyyMMdd,注意字母y不能大写。
 *
* @param sformat
 * yyyyMMddhhmmss
 * @return
 */
public static String getUserDate(String sformat) {
 Date currentTime = new Date();
 SimpleDateFormat formatter = new SimpleDateFormat(sformat);
 String dateString = formatter.format(currentTime);
 return dateString;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,Java 中可以使用 `SimpleDateFormat` 类来进行日期格式转换。 首先你需要创建一个 `SimpleDateFormat` 对象,并设置好转换的格式,例如: ```java SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); ``` 然后,你可以使用 `parse` 方法将字符串类型的日期转换为 `Date` 类型,例如: ```java String dateString = "2022-06-15 15:43:01"; Date date = sdf.parse(dateString); ``` 如果你想将 `Date` 类型的日期转换为字符串类型,可以使用 `format` 方法,例如: ```java Date date = new Date(); String dateString = sdf.format(date); ``` 希望这些信息能够帮到你! ### 回答2: Java提供了一种功能强大的日期格式转换功能,可以方便地将不同格式的日期进行转换。主要是通过SimpleDateFormat类来实现。 SimpleDateFormat类是DateFormat的一个具体子类,它可以用于格式化日期(即将日期转换为指定格式的字符串)和解析日期(即将字符串解析为日期对象)。 要进行日期格式转换,首先需要创建一个SimpleDateFormat对象,并指定要转换的日期格式。例如,可以使用"yyyy-MM-dd"表示年-月-日的格式,"HH:mm:ss"表示时:分:秒的格式。 比如,要将日期对象转换为指定格式的字符串,可以使用SimpleDateFormat的format()方法。例如: ``` SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String strDate = sdf.format(new Date()); ``` 这样就会将当前日期转换为"yyyy-MM-dd"格式的字符串。 另外,要将字符串解析为日期对象,可以使用SimpleDateFormat的parse()方法。例如: ``` SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date date = sdf.parse("2022-12-31"); ``` 这样就会将字符串"2022-12-31"解析为对应的日期对象。 需要注意的是,日期格式中的字母必须与日期字符串的格式一一对应,否则会抛出解析异常。所以,在进行日期格式转换时,要确保指定的格式与实际的日期字符串格式相匹配。 除了提供预定义的日期格式外,SimpleDateFormat还支持自定义日期格式,可以通过在日期格式字符串中添加特定的字符来表示各个日期部分的格式。 综上所述,通过Java的SimpleDateFormat类,可以方便地进行日期格式的转换,实现日期对象与字符串之间的相互转换。 ### 回答3: Java日期格式转换可以通过Java中提供的DateFormat类来实现。 DateFormat类是一个抽象类,它可以将日期和时间按照指定的格式进行格式化输出,也可以将格式化后的日期和时间字符串解析为Date对象。 日期格式转换的一般流程如下: 1. 创建DateFormat对象,指定日期格式的模式。 SimpleDateFormat类是DateFormat的一个实现类,通过它可以指定具体的日期格式模式。 2. 调用DateFormat的format()方法进行日期格式化,将Date对象转换为指定格式的日期字符串。 该方法需要传入一个Date对象作为参数,并返回一个格式化后的日期字符串。 3. 调用DateFormat的parse()方法进行日期解析,将格式化后的日期字符串转换为Date对象。 该方法需要传入一个格式化后的日期字符串作为参数,并返回一个解析后的Date对象。 以下是一个简单的示例代码,将日期转换为指定格式的字符串,并将字符串解析为日期对象: ```java import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; public class DateConversionExample { public static void main(String[] args) { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date currentDate = new Date(); // 将日期转换为指定格式的字符串 String formattedDate = dateFormat.format(currentDate); System.out.println("格式化后的日期:" + formattedDate); try { // 将格式化后的日期字符串解析为日期对象 Date parsedDate = dateFormat.parse(formattedDate); System.out.println("解析后的日期:" + parsedDate); } catch (Exception e) { System.out.println("日期解析错误:" + e.getMessage()); } } } ``` 以上代码中,DateFormat对象使用了"yyyy-MM-dd HH:mm:ss"模式进行日期格式化和解析。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值