java时间格式转换
package org.shineway.com; import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar; /**
* 各类时间类型的转换方式
* @author mengzw
* @since 3.0 2014-2-28
*/
public class TimeUtil { public TimeUtil() {
} /**
* 将 Date 类型转换成String
* @param date 时间 Date 类型参数
* @return 返回为 yyyy-MM-dd HH:mm:ss 格式String
*/
public static String getTimeString(Date date) {
String s = "yyyy'-'MM'-'dd' 'HH:mm:ss";
SimpleDateFormat simpledateformat = new SimpleDateFormat(s);
return simpledateformat.format(date);
} /**
* 得到当前时间的时分秒
* @return HH:mm:ss格式的字符串
*/
public static String getOnlyCurrentTimeString() {
String s = "HH:mm:ss";
SimpleDateFormat simpledateformat = new SimpleDateFormat(s);
Calendar calendar = Calendar.getInstance();
return simpledateformat.format(calendar.getTime());
} /**
* 将字符串日期转换成Date类型
* @param s 类型格式:'yyyy-MM-dd' / 'yyyy-MM-dd HH:mm:ss'
* @param s1 需要转换的 String类型 日期
* @return Date 类型日期
*/
public static Date getString2Date(String s, String s1) {
Date date = null;
try {
date = (new SimpleDateFormat(s1)).parse(s);
} catch (Exception exception) {
date = null;
}
return date;
} /**
* 将 Date 类型转换成指定格式
* @param date 数据
* @param s 时间显示格式
* @return String日期字符串
*/
public static String getFormartString(Date date, String s) {
SimpleDateFormat simpledateformat = new SimpleDateFormat(s);
if (null != date)
return simpledateformat.format(date);
else
return null;
} /**
* 将 Calendar 类型转换为指定格式
* @param calendar 数据
* @param s 时间显示格式
* @return String日期字符串
*/
public static String getFormartString(Calendar calendar, String s) {
SimpleDateFormat simpledateformat = new SimpleDateFormat(s);
return simpledateformat.format(calendar.getTime());
} /**
* 将 Calendar 时间类型转换为String
* @param calendar
* @return 返回yyyy-MM-dd HH-mm-ss格式
*/
public static String getTimeString(Calendar calendar) {
String s = "yyyy'-'MM'-'dd' 'HH'-'mm'-'ss";
SimpleDateFormat simpledateformat = new SimpleDateFormat(s);
return simpledateformat.format(calendar.getTime());
} /**
* 将Calendar 时间类型转换成String
* @param calendar
* @return 返回 yyyy-MM-dd格式
*/
public static String getDateString(Calendar calendar) {
String s = "yyyy'-'MM'-'dd";
SimpleDateFormat simpledateformat = new SimpleDateFormat(s);
return simpledateformat.format(calendar.getTime());
} /**
* 将String 日期字符转换成Calendar类型
* @param s 时间类型数据
* @param s1 需要转换格式
* @return Calendar日期数据
*/
public static Calendar getCalendar(String s, String s1) {
SimpleDateFormat simpledateformat = new SimpleDateFormat(s1);
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(simpledateformat.parse(s));
} catch (ParseException parseexception) {
return null;
}
return calendar;
} /**
* 输入日期自动判断格式返回日期
* @param s 日期字符
* @return Calendar类型日期
*/
public static Calendar getCalendar(String s) {
int i = s.length();
switch (i) {
case 19: // '\023'
return getCalendar(s, "yyyy'-'MM'-'dd' 'HH:mm:ss"); case 10: // '\n'
return getCalendar(s, "yyyy'-'MM'-'dd");
}
return null;
} /**
* 得到当前为第几个季度
* @return
*/
public static String getCurrentSeason() {
String s = getFormartString(Calendar.getInstance(), "MM");
String s1 = "";
if (s.equals("01") || s.equals("02") || s.equals("03"))
s1 = "1";
else if (s.equals("04") || s.equals("05") || s.equals("06"))
s1 = "2";
else if (s.equals("07") || s.equals("08") || s.equals("09"))
s1 = "3";
else if (s.equals("10") || s.equals("11") || s.equals("12"))
s1 = "4";
return s1;
} public static int getWeekOfYear(Date date) {
GregorianCalendar gregoriancalendar = new GregorianCalendar();
gregoriancalendar.setFirstDayOfWeek(2);
gregoriancalendar.setMinimalDaysInFirstWeek(7);
gregoriancalendar.setTime(date);
return gregoriancalendar.get(3);
} /**
* 判断是否为闰年
* @param i 年份
* @return 是闰年为true
*/
public static boolean isLeapYear(int i) {
boolean flag = false;
if (i % 4 == 0 && i % 100 != 0)
flag = true;
else if (i % 400 == 0)
flag = true;
else
flag = false;
return flag;
} }
java时间格式转换的更多相关文章
- java时间格式转换任意格式
例如:20180918/120023转换成2018-09-18 12:00:23 //时间格式转换 public String getNomalTime(String oldTime){ String ...
- JAVA时间格式转换大全
import java.text.*; import java.util.Calendar; public class VeDate { /** * 获取现在时间 * * @return 返回时间类型 ...
- Java时间格式转换工具类
把当前时间修改成指定时间 //把当前时间修改成指定时间 public String dateUtil(Integer seconds, String dateFormatPattern){ Date ...
- Java日期格式转换
Java时间格式转换大全 import java.text.*;import java.util.Calendar;public class VeDate {/** * 获取现在时间 * ...
- Java之格林威治时间格式转换成北京时间格式
Java之格林威治时间格式转换成北京时间格式 package com.mtons.mblog; import java.text.ParseException; import java.text.Si ...
- java时间格式大全
java.util.*;import java.text.*;import java.util.Calendar; public class VeDate { /** * 获取现在时间 * * ...
- scala 时间格式转换(String、Long、Date)
1)scala 时间格式转换(String.Long.Date) 1.时间字符类型转Date类型 [java] view plain copy import java.text.SimpleDateF ...
- scala 时间,时间格式转换
scala 时间,时间格式转换 1.scala 时间格式转换(String.Long.Date) 1.1时间字符类型转Date类型 1.2Long类型转字符类型 1.3时间字符类型转Long类型 2. ...
- SQL Server日期时间格式转换字符串详解 (详询请加qq:2085920154)
在SQL Server数据库中,SQL Server日期时间格式转换字符串可以改变SQL Server日期和时间的格式,是每个SQL数据库用户都应该掌握的.本文我们主要就介绍一下SQL Server日 ...
随机推荐
- [Redux] Generating Containers with connect() from React Redux (VisibleTodoList)
Learn how to use the that comes with React Redux instead of the hand-rolled implementation from the ...
- js_day8
- avalon学习笔记一 列表及条件过滤
好长时间都没有更新博客了,不是因为没有学习新的东西,而是到了新的单位每天玩命加班实在是太累了!经过一年的努力吧,终于可以轻松一下了.废话少说,直接干货吧! 由于是学习阶段,就直接拿了公司的二级页面做了 ...
- PHP上传文件DEMO
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"> <html> <head> ...
- hdu1054 树状dp
B - 树形dp Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:10000KB 64bit ...
- hdu Find a way
算法:广搜: Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Le ...
- Linux Shell(初识)
什么是Shell:Shell是一个命令解释器. Linux下支持shell的种类: 1. Bourne Shell(简称sh) 2.C Shell(简称csh) 3.Korn Shell(简称ksh ...
- hdfs-over-ftp安装与配置
hdfs-over-ftp是一个开源,简单易用的实现了对HDFS系统的下载和上传功能的小工具.可以作为管理工具来使用,快捷方便. 1 安装jdk(1.6以上版本)并配置环境变量分别执行java -ve ...
- Arbitrage HDU
Arbitrage Time Limit: 2000/1000 MS (Java/Others) Mem ...
- memcached在Windows下的安装
memcached简介详情请谷歌.这里介绍如何在windows下安装. 1.下载 下载地址:http://download.csdn.net/detail/u010562988/9456109 ...