纵有疾风起
人生不言弃

java日期转化

package com.kang.util;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;public class CalendarTest {public static void main(String[] args) throws ParseException {// 获取当前年份、月份、日期Calendar cale = null;cale = Calendar.getInstance();int year = cale.get(Calendar.YEAR);int month = cale.get(Calendar.MONTH) + 1;int day = cale.get(Calendar.DATE);int hour = cale.get(Calendar.HOUR_OF_DAY);int minute = cale.get(Calendar.MINUTE);int second = cale.get(Calendar.SECOND);int dow = cale.get(Calendar.DAY_OF_WEEK);int dom = cale.get(Calendar.DAY_OF_MONTH);int doy = cale.get(Calendar.DAY_OF_YEAR);System.out.println("Current Date: " + cale.getTime());System.out.println("Year: " + year);System.out.println("Month: " + month);System.out.println("Day: " + day);System.out.println("Hour: " + hour);System.out.println("Minute: " + minute);System.out.println("Second: " + second);System.out.println("Day of Week: " + dow);System.out.println("Day of Month: " + dom);System.out.println("Day of Year: " + doy);// 获取当月第一天和最后一天SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");String firstday, lastday;// 获取前月的第一天cale = Calendar.getInstance();cale.add(Calendar.MONTH, 0);cale.set(Calendar.DAY_OF_MONTH, 1);firstday = format.format(cale.getTime());// 获取前月的最后一天cale = Calendar.getInstance();cale.add(Calendar.MONTH, 1);cale.set(Calendar.DAY_OF_MONTH, 0);lastday = format.format(cale.getTime());System.out.println("本月第一天和最后一天分别是 : " + firstday + " and " + lastday);// 获取当前日期字符串Date d = new Date();System.out.println("当前日期字符串1:" + format.format(d));System.out.println("当前日期字符串2:" + year + "/" + month + "/" + day + " "+ hour + ":" + minute + ":" + second);// 字符串格式转化,获取年份月份或其他String ss = "2014-08";Calendar Date = Calendar.getInstance();SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM"); // yyyy-MM-dd// hh:mm:ssDate.setTime(simpleDate.parse(ss));// Date和以上的Cale的使用方法相同int days = Date.getActualMaximum(Calendar.DAY_OF_MONTH);// 获取当前月的天数int month1 = Date.get(Calendar.MONTH);// 获取当前月的天数// 获取前一天、前一个月的日期Calendar calendar = Calendar.getInstance();// 此时打印它获取的是系统当前时间calendar.add(Calendar.DATE, -1); // 得到前一天String yestedayDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());calendar.add(Calendar.MONTH, -1); // 得到前一个月int year2 = calendar.get(Calendar.YEAR);int month2 = calendar.get(Calendar.MONTH) + 1; // 输出前一月的时候要记得加1}}

  

文章转载于:https://www.cnblogs.com/kxdblog/p/4077437.html

原著是一个有趣的人,若有侵权,请通知删除

未经允许不得转载:起风网 » java日期转化
分享到: 生成海报

评论 抢沙发

评论前必须登录!

立即登录