`
myrl1023
  • 浏览: 35194 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

java--TimeTools

阅读更多

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Title: TimeTools.java Description: Copyright: Copyright (c) 2011 Company:
 * company
 * 
 * @author channing
 * @date 2011-5-25 下午03:13:32
 * @version 1.0
 */
public class TimeTools {
	public final static String defaultFormat = "yyyy-MM-dd HH:mm:ss";

	public enum DateFiled {
		YEAR,
		MONTH,
		DATE,
		HOUR,
		MINUTE
	}

	/**
	 * 
	 * Title: getCurrentTime 
	 * Description: 格式化时间 为String 类型
	 * 
	 * @param format
	 * @return
	 */
	public static String getCurrentTime(String format) {
		SimpleDateFormat fmt = new SimpleDateFormat((format != null && !""
				.equals(format)) ? format : defaultFormat);
		return fmt.format(Calendar.getInstance().getTime());
	}

	/**
	 * 
	 * Title: getCurrentDate 
	 * Description: 得到当前时间
	 * 
	 * @return
	 */
	public static Date getCurrentDate() {
		return Calendar.getInstance().getTime();
	}

	/**
	 * 
	 * Title: getMonthFirstDay 
	 * Description:得到本月的第一天 传入"yyyy-MM-dd"
	 * 
	 * @param format
	 * @return
	 */
	public static String getMonthFirstDay(String format) {
		SimpleDateFormat fmt = new SimpleDateFormat(format);
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, calendar
				.getActualMinimum(Calendar.DAY_OF_MONTH));

		return fmt.format(calendar.getTime());
	}

	/**
	 * 
	 * Title: getCurrentMonthLisatDay 
	 * Description: 当月最后一天
	 * 
	 * @param format
	 * @return
	 */
	public static String getCurrentMonthLisatDay(String format) {
		SimpleDateFormat fmt = new SimpleDateFormat((format != null && !""
				.equals(format)) ? format : defaultFormat);

		Calendar cal = Calendar.getInstance();// 获取当前日期
		cal.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		cal.add(Calendar.MONTH, 1);// 月增加1天
		cal.add(Calendar.DAY_OF_MONTH, -1);// 日期倒数一日,既得到本月最后一天
		return fmt.format(cal.getTime());
	}

	/**
	 * 
	 * Title: fmtDate 
	 * Description: 格式化时间 date 格式为 fmt
	 * 
	 * @param date
	 * @param fmt
	 * @return
	 */
	public static String fmtDate(Date date, String fmt) {
		if (date == null)
			return null;
		SimpleDateFormat sdf = new SimpleDateFormat((fmt != null && !""
				.equals(fmt)) ? fmt : defaultFormat);
		return sdf.format(date);
	}

	/**
	 * 
	 * Title: getTime 
	 * Description: 格式化时间 string time 格式为 fmt 返回 date 类型
	 * 
	 * @param time
	 * @param fmt
	 * @return
	 */
	public static Date getTime(String time, String fmt) {
		SimpleDateFormat sdf = new SimpleDateFormat((fmt != null && !""
				.equals(fmt)) ? fmt : defaultFormat);
		try {
			return sdf.parse(time);
		} catch (ParseException e) {
			System.out.println("String to Date Error:" + e.getMessage());
			return null;
		}
	}

	/**
	 * 
	 * Title: modifyDate 
	 * Description: 增加年、月、日
	 * 
	 * @param date
	 * @param amount
	 * @param filed
	 * @return
	 */
	public static Date modifyDate(Date date, int amount, DateFiled filed) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		if (filed == DateFiled.YEAR) {
			c.add(Calendar.YEAR, amount);
		} else if (filed == DateFiled.MONTH) {
			c.add(Calendar.MONTH, amount);
		} else if (filed == DateFiled.DATE) {
			c.add(Calendar.DAY_OF_MONTH, amount);
		} else if (filed == DateFiled.HOUR) {
			c.add(Calendar.HOUR, amount);
		} else if (filed == DateFiled.MINUTE) {
			c.add(Calendar.MINUTE, amount);
		}
		return c.getTime();
	}

	/**
	 * 
	 * Title: getBussinessNoByDate 
	 * Description: bussiness no Date
	 * 
	 * @return
	 */
	public static String getBussinessNoByDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmssSS");
		return sdf.format(getCurrentDate());
	}

	/**
	 * 
	 * Title: getfmtDateStr 
	 * Description:
	 * 
	 * @param dateStr
	 * @param dateStrFmt
	 * @param fmt
	 * @return
	 */
	public static String getfmtDateStr(String dateStr, String dateStrFmt,
			String fmt) {

		DateFormat df2 = (null == dateStrFmt) ? new SimpleDateFormat(
				defaultFormat) : new SimpleDateFormat(dateStrFmt);
		try {
			Date date2 = df2.parse(dateStr);
			return fmtDate(date2, fmt);
		} catch (ParseException e) {
			System.out.println("String time  Error:" + e.getMessage());
		}
		return null;
	}

	public static void main(String[] args) {
		String time = TimeTools.getBussinessNoByDate();
		System.out.println(time);
	}
}



分享到:
评论

相关推荐

    JAVA-Based Agent Platform Evaluation

    development tools (platforms) with respect to the specific requirements of agent-based manufacturing control solutions. We discuss the use of agents, as high-level decision-making entities, in ...

    Cloud-Native+Applications+in+Java-Packt+Publishing(2018).epub

    For this, they need to be aware of the environment, tools, and resources they're coding against. The book begins by explaining the driving factors for cloud adoption and shows you how cloud ...

    基于Java-Web的智能推荐网络教育平台的设计和实现.docx

    The specific development tools use the integrated development tool Myeclipse, and the Web-based B/S architecture. Mainly completed functions The front desk mainly includes functions such as user ...

    Java: High-Performance Apps with Java 9

    Java: High-Performance Apps with Java 9: Boost your application’s performance with the new features of Java 9 By 作者: Mayur Ramgir – Nick Samoylov ISBN-10 书号: 1789130514 ISBN-13 书号: ...

    eclipse-modeling-2022-06-R-linux-gtk-x86_64.tar.gz

    You can use it to graphically design domain models, to leverage those models at design time by creating and editing dynamic instances, to collaborate via Eclipse's team support with facilities for ...

    Mastering Java Machine Learning

    Build high-performing, real-time, adaptive predictive models for batch- and stream-based big data learning using the latest tools and methodologies. Get a deeper understanding of technologies leading ...

    Test-Driven Java Development

    You'll be guided through setting up tools, frameworks, and the environment you need, and will dive right in to hands-on exercises with the goal of mastering one practice, tool, or framework at a time....

    Test-Driven Java Development - Second Edition.pdf

    day routine.You'll be guided through setting up tools, frameworks, and the environment you need, and we will dive right into hands-on exercises with the goal of mastering one practice, tool, or ...

    Java2核心技术卷I+卷2:基础知识(第8版) 代码

    Using the Command-Line Tools 22 Using an Integrated Development Environment 25 Running a Graphical Application 28 Building and Running Applets 31 Chapter 3: Fundamental Programming Structures ...

    Test-Driven Java Development(PACKT,2015)

    You’ll be guided through setting up tools, frameworks, and the environment you need, and will dive right in to hands-on exercises with the goal of mastering one practice, tool, or framework at a time...

    Prentice Hall - Java Performance

    Tuning the Java HotSpot VM for startup, memory footprint, response time, and latency Determining when Java applications require rework to meet performance goals Systematically profiling and tuning ...

    Android代码-jvm-tools

    Swiss Java Knife (SJK) SJK is a command line tool for JVM diagnostic, troubleshooting and profiling. SJK exploits standard diagnostic ...Monitor Java threads in real time Analyze head dynamics w

    Java Test-Driven Development(PACKT,2015)

    You'll be guided through setting up tools, frameworks, and the environment you need, and will dive right in to hands-on exercises with the goal of mastering one practice, tool, or framework at a time....

    java源码剖析-java-code-analysis:使用各种静态,动态,错误和测试指标分析源代码中的故障检测

    Time , Lang , Chart , Closure和Mockito的结果。 在此基础上,我通过各种ML技术预测了故障定位的质量(using和dstar2)。 可以在分析/文件夹中找到。 用法 这个Maven项目需要JAVA 8 。 如下放置错误版本: src /...

    JADE-all-4.1.zip_Agent_JADE4.1_Multi Agent System_jade java_jade

    is a software framework fully implemented in Java language.It simplifies the implementation of multi-agent systems through a middle-ware that claims to comply with the FIPA specifications and through ...

    Packt.Java9.Cookbook

    This cookbook offers a range of software development examples in simple and straightforward Java 9 code, providing step-by-step resources and time-saving methods to help you solve data problems ...

    Java_Performance

    Ø Using modern Java Virtual Machine (JVM) and OS observability tools to profile running systems, with almost no performance penalty Ø Gaining “under the hood” knowledge of the Java HotSpot VM that...

    Microsoft Visual Studio

    Common\Graphics\bitmaps\assorted\time.bmp Common\Graphics\bitmaps\assorted\trash.bmp Common\Graphics\bitmaps\assorted\vb416mom.bmp Common\Graphics\bitmaps\assorted\vb432mom.bmp Common\Graphics\...

    Java Performance

    Tuning the Java HotSpot VM for startup, memory footprint, response time, and latency Determining when Java applications require rework to meet performance goals Systematically profiling and tuning ...

    java.核心技术.第八版 卷1

    Using the Command-Line Tools 22 Using an Integrated Development Environment 25 Running a Graphical Application 28 Building and Running Applets 31 Chapter 3: Fundamental Programming Structures in ...

Global site tag (gtag.js) - Google Analytics