DateUtil工具类

package com.tmeos.xueliang.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import android.annotation.SuppressLint;

public class DateUtil
{
	private DateUtil()
	{
		throw new AssertionError();
	}

	public static long getCurrentData()
	{
		return System.currentTimeMillis();
	}

	@SuppressLint( "SimpleDateFormat" )
	public static String getCurformatData()
	{
		Calendar currentDate = Calendar.getInstance();
		String str = ( new SimpleDateFormat( "yyyy.MM.dd HH:mm E" ) )
						.format( currentDate.getTime() );
		return str;
	}

	@SuppressLint( "SimpleDateFormat" )
	public static String getCurformatDataNoWeek()
	{

		Calendar currentDate = Calendar.getInstance();
		String str = ( new SimpleDateFormat( "yyyyMMdd" ) ).format( currentDate.getTime() );
		return str;
	}

	public static long getDataBeginTime()
	{

		Calendar currentDate = Calendar.getInstance();
		currentDate.set( Calendar.HOUR_OF_DAY, 0 );
		currentDate.set( Calendar.MINUTE, 0 );
		currentDate.set( Calendar.SECOND, 0 );
		currentDate.set( Calendar.MILLISECOND, 0 );
		return currentDate.getTimeInMillis();

	}

	// 把日期转为字符串
	@SuppressLint( "SimpleDateFormat" )
	public static String converToString( Date date )
	{
		DateFormat df = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
		return df.format( date );
	}

	// 把字符串转为日期
	public static Date converToDate( String strDate ) throws Exception
	{
		DateFormat df = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
		return df.parse( strDate );
	}

	public static String converToStringWithFormatter( Date date, String formatter )
	{
		DateFormat df = new SimpleDateFormat( formatter );
		return df.format( date );
	}

	public static String getHourMinSec( long ms )
	{
		Integer ss = 1000;
		Integer mi = ss * 60;
		Integer hh = mi * 60;

		Long hour = ms / hh;
		Long minute = ( ms - hour * hh ) / mi;
		Long second = ( ms - hour * hh - minute * mi ) / ss;

		StringBuffer sb = new StringBuffer();
		sb.append( String.format( "%02d", hour ) + ":" );
		sb.append( String.format( "%02d", minute ) + ":" );
		sb.append( String.format( "%02d", second ) );

		return sb.toString();
	}

	@SuppressLint( "SimpleDateFormat" )
	public static List< String > get7DaysAgoAndWeek()
	{
		List< String > list = new ArrayList< String >();
		SimpleDateFormat sdf = new SimpleDateFormat( "MM.dd" );
		SimpleDateFormat sdf_week = new SimpleDateFormat( "E" );

		for ( int i = 0; i < 7; i++ )
		{

			Calendar c = Calendar.getInstance();
			if ( i == 0 )
			{
				c.add( Calendar.DATE, 0 );
				list.add( sdf_week.format( c.getTime() ) );
			}
			else if ( i == 1 )
			{
				c.add( Calendar.DATE, -1 );
				list.add( sdf_week.format( c.getTime() ) );
			}
			else if ( i == 2 )
			{

				c.add( Calendar.DATE, -2 );
				list.add( sdf_week.format( c.getTime() ) );
			}
			else
			{
				c.add( Calendar.DATE, -i );
				list.add( sdf_week.format( c.getTime() ) );
			}

		}
		return list;

	}

	@SuppressLint( "SimpleDateFormat" )
	public static List< String > get7DaysAgoAndWeek_()
	{
		List< String > list = new ArrayList< String >();
		SimpleDateFormat sdf = new SimpleDateFormat( "MM-dd" );
		SimpleDateFormat sdf_week = new SimpleDateFormat( "E" );

		for ( int i = 0; i < 7; i++ )
		{

			Calendar c = Calendar.getInstance();
			if ( i == 0 )
			{
				c.add( Calendar.DATE, 0 );
				list.add( "今天" );
			}
			else if ( i == 1 )
			{
				c.add( Calendar.DATE, -1 );
				list.add( sdf.format( c.getTime() ) );
			}
			else if ( i == 2 )
			{

				c.add( Calendar.DATE, -2 );
				list.add( sdf.format( c.getTime() ) );
			}
			else
			{
				c.add( Calendar.DATE, -i );
				list.add( sdf.format( c.getTime() ) );
			}

		}
		return list;

	}

	@SuppressLint( "SimpleDateFormat" )
	public static String[] getSevenWeek()
	{
		String[] weekStrings = new String[ 7 ];
		SimpleDateFormat sdf_week = new SimpleDateFormat( "E" );

		for ( int i = 0; i < 7; i++ )
		{

			Calendar c = Calendar.getInstance();
			if ( i == 0 )
			{
				c.add( Calendar.DATE, 0 );
				weekStrings[i] = sdf_week.format( c.getTime() );
			}
			else if ( i == 1 )
			{
				c.add( Calendar.DATE, -1 );
				weekStrings[i] = sdf_week.format( c.getTime() );
			}
			else if ( i == 2 )
			{

				c.add( Calendar.DATE, -2 );
				weekStrings[i] = sdf_week.format( c.getTime() );
			}
			else
			{
				c.add( Calendar.DATE, -i );
				weekStrings[i] = sdf_week.format( c.getTime() );
			}

		}
		return weekStrings;

	}

	/*
	 * 获取今天之前的七天(包括今天)的日期(只显示到某日)
	 */
	@SuppressLint( "SimpleDateFormat" )
	public static List< String > get7DaysAgo()
	{

		List< String > list = new ArrayList< String >();
		SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" );
		for ( int i = 0; i < 7; i++ )
		{

			Calendar c = Calendar.getInstance();
			c.add( Calendar.DATE, -i );
			list.add( sdf.format( c.getTime() ) );

		}
		return list;
	}

	/*
	 * 获得前七天那天是第几日
	 */
	public static long get7TheDay()
	{

		Calendar c = Calendar.getInstance();
		c.add( Calendar.DAY_OF_MONTH, -7 );
		c.set( Calendar.MINUTE, 0 );
		c.set( Calendar.SECOND, 0 );
		c.set( Calendar.MILLISECOND, 0 );
		return c.getTimeInMillis();

	}

	/*
	 * 毫秒的日期转为日期类型
	 */
	public static String milliDate2Day( long date )
	{

		Date dat = new Date( date );
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime( dat );
		java.text.SimpleDateFormat format = new java.text.SimpleDateFormat( "dd" );
		String day = format.format( gc.getTime() );

		return day;

	}

	/*
	 * 获得前20天那天是第几日
	 */
	public static long get20TheDay()
	{
		Calendar c = Calendar.getInstance();
		c.add( Calendar.DAY_OF_MONTH, -20 );
		c.set( Calendar.MINUTE, 0 );
		c.set( Calendar.SECOND, 0 );
		c.set( Calendar.MILLISECOND, 0 );
		return c.getTimeInMillis();

	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值