JAVA 的data类型 long类型 生成星期几汇总

本文介绍了一个Java实用类TimeUtil,用于进行系统时间获取、格式化时间显示、判断星期几等操作。通过使用SimpleDateFormat和Calendar类,该类能够提供如当前时间的长整型值、格式化为年月日时分秒的时间字符串、以及判断具体日期是星期几等功能。

**
*
* 时间的操作类
*
* */
public class TimeUtil {

/**
* 返回系统时间
*
* @param void
* @return 系统时间 long
*
* */
public static long CurrentTime() {

return System.currentTimeMillis();

}

/**
* 返回年-月-日 hh:ff:ss
*
* @param time
* long系统时间的long类型
* @return String yyyy-MM-dd HH:mm:ss类型的时间类型
* */
public static String getFormatTime(long time) {

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Date date = new Date(time);

String formatTime = format.format(date);

return formatTime;

}

/**
* 星期几
*
* @param data
* Date 日期
* @return 星期一到星期日
*
* */
public static String getWeekOfDate(Date date) {
String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;

return weekDays[w];
}

/**
* 星期几
*
* @param time
* long 系统时间的long类型
* @return 星期一到星期日
*
* */
public static String getWeekOfDate(long time) {

Date date = new Date(time);
String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;

return weekDays[w];
}

/**
* 返回年-月-日 hh:ff:ss 星期几
*
* @param time
* long系统时间
* @return String 例如2013-05-26 19:39:26 星期日
*
* */

public static String getDateAndWeek(long time) {

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Date date = new Date(time);

String dateString = format.format(date);

String dayString = getWeekOfDate(date);

return dateString + " " + dayString;

}

}

转载于:https://www.cnblogs.com/qgzhan/archive/2013/05/26/3100319.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值