📄 dateformatbystring.java
字号:
package com.saas.biz.commen;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import com.saas.sys.exp.SaasApplicationException;
/**
* @author:LiuYang
* @desc:日期类型格式化
* @2008-5-19
*/
public class DateFormatByString {
/**
* 返回年月日时分秒星期
*
* @param data
* @return
* @throws ParseException
*/
public String getDateYMDHMSW(String data) throws ParseException {
Calendar calendar = Calendar.getInstance();
String format = "";
if (data.length() > 10) {
format = "yyyy-MM-dd HH:mm:ss";
}
else {
format = "yyyy-MM-dd";
}
SimpleDateFormat myFmt = new SimpleDateFormat(format);
Date date = myFmt.parse(data);
calendar.setTime(date);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
String strDate = year + "年" + month + "月" + day + "日" + hour + "时" + minute + "分" + second + "秒" + getWeek(week);
return strDate;
}
public static void main(String[] args) throws SaasApplicationException {
System.out.println(new DateFormatByString().getDateTimeNow());
}
/**
* @return 取出当前日期
* @throws SaasApplicationException
*/
public String getDateTimeNow() throws SaasApplicationException {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
String strDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + " " + getWeek(week);
return strDate;
}
/**
* 返回星期
*
* @param week
* @return
*/
public String getWeek(int week) {
int temp = week;
String weekday = "";
if (temp == 1)
weekday = "星期一";
else if (temp == 2)
weekday = "星期二";
else if (temp == 3)
weekday = "星期三";
else if (temp == 4)
weekday = "星期四";
else if (temp == 5)
weekday = "星期五";
else if (temp == 6)
weekday = "星期六";
else
weekday = "星期日";
return weekday;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -