Java时间日期格式转换

在F1C100s上跑GBA游戏:手把手教你用Buildroot配置SDL和移植gpsp模拟器 本文详细介绍了在全志F1C100s开发板上配置Buildroot、移植SDL库和gpsp模拟器的完整流程,帮助开发者实现在ARM9平台上流畅运行GBA游戏。内容涵盖环境配置、交叉编译、常见问题解决及性能优化技巧,特别针对'段错误'和'无声音'等典型问题提供实用解决方案。 阅读详情

Edward的成长日记

专注Springsource产品研究

随笔 - 125  文章 - 0  评论 - 30

 

突然忘记了时间格式怎么转换,特此做个记录

 

Java时间格式转换大全

import java.text.*;
import java.util.Calendar;
public class VeDate {
/**
   * 获取现在时间
   * 
   * @return 返回时间类型 yyyy-MM-dd HH:mm:ss
   */
public static Date getNowDate() {
   Date currentTime = new Date();
   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   String dateString = formatter.format(currentTime);
   ParsePosition pos = new ParsePosition(8);
   Date currentTime_2 = formatter.parse(dateString, pos);
   return currentTime_2;
}
/**
   * 获取现在时间
   * 
   * @return返回短时间格式 yyyy-MM-dd
   */
DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");         
DateFormat format 2= new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");         
Date date = null;    
String str = null;                  
            
// String转Date    
str = "2007-1-18";          
try {    
           date = format1.parse(str);   
           data = format2.parse(str); 
} catch (ParseException e) {    
           e.printStackTrace();    
}   
/**
   * 获取现在时间
   * 
   * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
   */
public static String getStringDate() {
   Date currentTime = new Date();
   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   String dateString = formatter.format(currentTime);
   return dateString;
}
/**
   * 获取现在时间
   * 
   * @return 返回短时间字符串格式yyyy-MM-dd
   */
public static String getStringDateShort() {
   Date currentTime = new Date();
   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
   String dateString = formatter.format(currentTime);
   return dateString;
}
/**
   * 获取时间 小时:分;秒 HH:mm:ss
   * 
   * @return
   */
public static String getTimeShort() {
   SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
   Date currentTime = new Date();
   String dateString = formatter.format(currentTime);
   return dateString;
}
/**
   * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
   * 
   * @param strDate
   * @return
   */
public static Date strToDateLong(String strDate) {
   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   ParsePosition pos = new ParsePosition(0);
   Date strtodate = formatter.parse(strDate, pos);
   return strtodate;
}
/**
   * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
   * 
   * @param dateDate
   * @return
   */
public static String dateToStrLong(java.util.Date dateDate) {
   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   String dateString = formatter.format(dateDate);
   return dateString;
}
/**
   * 将短时间格式时间转换为字符串 yyyy-MM-dd
   * 
   * @param dateDate
   * @param k
   * @return
   */
public static String dateToStr(java.util.Date dateDate) {
   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
   String dateString = formatter.format(dateDate);
   return dateString;
}
/**
   * 将短时间格式字符串转换为时间 yyyy-MM-dd 
   * 
   * @param strDate
   * @return
   */
public static Date strToDate(String strDate) {
   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
   ParsePosition pos = new ParsePosition(0);
   Date strtodate = formatter.parse(strDate, pos);
   return strtodate;
}
/**
   * 得到现在时间
   * 
   * @return
   */
public static Date getNow() {
   Date currentTime = new Date();
   return currentTime;
}
/**
   * 提取一个月中的最后一天
   * 
   * @param day
   * @return
   */
public static Date getLastDate(long day) {
   Date date = new Date();
   long date_3_hm = date.getTime() - 3600000 * 34 * day;
   Date date_3_hm_date = new Date(date_3_hm);
   return date_3_hm_date;
}
/**
   * 得到现在时间
   * 
   * @return 字符串 yyyyMMdd HHmmss
   */
public static String getStringToday() {
   Date currentTime = new Date();
   SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");
   String dateString = formatter.format(currentTime);
   return dateString;
}
/**
   * 得到现在小时
   */
public static String getHour() {
   Date currentTime = new Date();
   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   String dateString = formatter.format(currentTime);
   String hour;
   hour = dateString.substring(11, 13);
   return hour;
}
/**
   * 得到现在分钟
   * 
   * @return
   */
public static String getTime() {
   Date currentTime = new Date();
   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   String dateString = formatter.format(currentTime);
   String min;
   min = dateString.substring(14, 16);
   return min;
}
/**
   * 根据用户传入的时间表示格式,返回当前时间的格式 如果是yyyyMMdd,注意字母y不能大写。
   * 
   * @param sformat
   *             yyyyMMddhhmmss
   * @return
   */
public static String getUserDate(String sformat) {
   Date currentTime = new Date();
   SimpleDateFormat formatter = new SimpleDateFormat(sformat);
   String dateString = formatter.format(currentTime);
   return dateString;
}

--------------------------------------------------------------------------------------------------------------------------------

做成方法

import java.util.*;
import java.text.*;
import java.util.Calendar;

public class VeDate {
 /**
  * 获取现在时间
  * 
  * @return 返回时间类型 yyyy-MM-dd HH:mm:ss
  */
 public static Date getNowDate() {
  Date currentTime = new Date();
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  String dateString = formatter.format(currentTime);
  ParsePosition pos = new ParsePosition(8);
  Date currentTime_2 = formatter.parse(dateString, pos);
  return currentTime_2;
 }

 /**
  * 获取现在时间
  * 
  * @return返回短时间格式 yyyy-MM-dd
  */
 public static Date getNowDateShort() {
  Date currentTime = new Date();
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  String dateString = formatter.format(currentTime);
  ParsePosition pos = new ParsePosition(8);
  Date currentTime_2 = formatter.parse(dateString, pos);
  return currentTime_2;
 }

 /**
  * 获取现在时间
  * 
  * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
  */
 public static String getStringDate() {
  Date currentTime = new Date();
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  String dateString = formatter.format(currentTime);
  return dateString;
 }

 /**
  * 获取现在时间
  * 
  * @return 返回短时间字符串格式yyyy-MM-dd
  */
 public static String getStringDateShort() {
  Date currentTime = new Date();
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  String dateString = formatter.format(currentTime);
  return dateString;
 }

 /**
  * 获取时间 小时:分;秒 HH:mm:ss
  * 
  * @return
  */
 public static String getTimeShort() {
  SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
  Date currentTime = new Date();
  String dateString = formatter.format(currentTime);
  return dateString;
 }

 /**
  * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
  * 
  * @param strDate
  * @return
  */
 public static Date strToDateLong(String strDate) {
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  ParsePosition pos = new ParsePosition(0);
  Date strtodate = formatter.parse(strDate, pos);
  return strtodate;
 }

 /**
  * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
  * 
  * @param dateDate
  * @return
  */
 public static String dateToStrLong(java.util.Date dateDate) {
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  String dateString = formatter.format(dateDate);
  return dateString;
 }

 /**
  * 将短时间格式时间转换为字符串 yyyy-MM-dd
  * 
  * @param dateDate
  * @param k
  * @return
  */
 public static String dateToStr(java.util.Date dateDate) {
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  String dateString = formatter.format(dateDate);
  return dateString;
 }

 /**
  * 将短时间格式字符串转换为时间 yyyy-MM-dd 
  * 
  * @param strDate
  * @return
  */
 public static Date strToDate(String strDate) {
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  ParsePosition pos = new ParsePosition(0);
  Date strtodate = formatter.parse(strDate, pos);
  return strtodate;
 }

 /**
  * 得到现在时间
  * 
  * @return
  */
 public static Date getNow() {
  Date currentTime = new Date();
  return currentTime;
 }

 /**
  * 提取一个月中的最后一天
  * 
  * @param day
  * @return
  */
 public static Date getLastDate(long day) {
  Date date = new Date();
  long date_3_hm = date.getTime() - 3600000 * 34 * day;
  Date date_3_hm_date = new Date(date_3_hm);
  return date_3_hm_date;
 }

 /**
  * 得到现在时间
  * 
  * @return 字符串 yyyyMMdd HHmmss
  */
 public static String getStringToday() {
  Date currentTime = new Date();
  SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");
  String dateString = formatter.format(currentTime);
  return dateString;
 }

 /**
  * 得到现在小时
  */
 public static String getHour() {
  Date currentTime = new Date();
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  String dateString = formatter.format(currentTime);
  String hour;
  hour = dateString.substring(11, 13);
  return hour;
 }

 /**
  * 得到现在分钟
  * 
  * @return
  */
 public static String getTime() {
  Date currentTime = new Date();
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  String dateString = formatter.format(currentTime);
  String min;
  min = dateString.substring(14, 16);
  return min;
 }

 /**
  * 根据用户传入的时间表示格式,返回当前时间的格式 如果是yyyyMMdd,注意字母y不能大写。
  * 
  * @param sformat
  *            yyyyMMddhhmmss
  * @return
  */
 public static String getUserDate(String sformat) {
  Date currentTime = new Date();
  SimpleDateFormat formatter = new SimpleDateFormat(sformat);
  String dateString = formatter.format(currentTime);
  return dateString;
 }

 /**
  * 二个小时时间间的差值,必须保证二个时间都是"HH:MM"的格式,返回字符型的分钟
  */
 public static String getTwoHour(String st1, String st2) {
  String[] kk = null;
  String[] jj = null;
  kk = st1.split(":");
  jj = st2.split(":");
  if (Integer.parseInt(kk[0]) < Integer.parseInt(jj[0]))
   return "0";
  else {
   double y = Double.parseDouble(kk[0]) + Double.parseDouble(kk[1]) / 60;
   double u = Double.parseDouble(jj[0]) + Double.parseDouble(jj[1]) / 60;
   if ((y - u) > 0)
    return y - u + "";
   else
    return "0";
  }
 }

 /**
  * 得到二个日期间的间隔天数
  */
 public static String getTwoDay(String sj1, String sj2) {
  SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
  long day = 0;
  try {
   java.util.Date date = myFormatter.parse(sj1);
   java.util.Date mydate = myFormatter.parse(sj2);
   day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
  } catch (Exception e) {
   return "";
  }
  return day + "";
 }

 /**
  * 时间前推或后推分钟,其中JJ表示分钟.
  */
 public static String getPreTime(String sj1, String jj) {
  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  String mydate1 = "";
  try {
   Date date1 = format.parse(sj1);
   long Time = (date1.getTime() / 1000) + Integer.parseInt(jj) * 60;
   date1.setTime(Time * 1000);
   mydate1 = format.format(date1);
  } catch (Exception e) {
  }
  return mydate1;
 }

 /**
  * 得到一个时间延后或前移几天的时间,nowdate为时间,delay为前移或后延的天数
  */
 public static String getNextDay(String nowdate, String delay) {
  try{
  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  String mdate = "";
  Date d = strToDate(nowdate);
  long myTime = (d.getTime() / 1000) + Integer.parseInt(delay) * 24 * 60 * 60;
  d.setTime(myTime * 1000);
  mdate = format.format(d);
  return mdate;
  }catch(Exception e){
   return "";
  }
 }

 /**
  * 判断是否润年
  * 
  * @param ddate
  * @return
  */
 public static boolean isLeapYear(String ddate) {

  /**
   * 详细设计: 1.被400整除是闰年,否则: 2.不能被4整除则不是闰年 3.能被4整除同时不能被100整除则是闰年
   * 3.能被4整除同时能被100整除则不是闰年
   */
  Date d = strToDate(ddate);
  GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
  gc.setTime(d);
  int year = gc.get(Calendar.YEAR);
  if ((year % 400) == 0)
   return true;
  else if ((year % 4) == 0) {
   if ((year % 100) == 0)
    return false;
   else
    return true;
  } else
   return false;
 }

 /**
  * 返回美国时间格式 26 Apr 2006
  * 
  * @param str
  * @return
  */
 public static String getEDate(String str) {
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  ParsePosition pos = new ParsePosition(0);
  Date strtodate = formatter.parse(str, pos);
  String j = strtodate.toString();
  String[] k = j.split(" ");
  return k[2] + k[1].toUpperCase() + k[5].substring(2, 4);
 }

 /**
  * 获取一个月的最后一天
  * 
  * @param dat
  * @return
  */
 public static String getEndDateOfMonth(String dat) {// yyyy-MM-dd
  String str = dat.substring(0, 8);
  String month = dat.substring(5, 7);
  int mon = Integer.parseInt(month);
  if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10 || mon == 12) {
   str += "31";
  } else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {
   str += "30";
  } else {
   if (isLeapYear(dat)) {
    str += "29";
   } else {
    str += "28";
   }
  }
  return str;
 }

 /**
  * 判断二个时间是否在同一个周
  * 
  * @param date1
  * @param date2
  * @return
  */
 public static boolean isSameWeekDates(Date date1, Date date2) {
  Calendar cal1 = Calendar.getInstance();
  Calendar cal2 = Calendar.getInstance();
  cal1.setTime(date1);
  cal2.setTime(date2);
  int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
  if (0 == subYear) {
   if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
    return true;
  } else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)) {
   // 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周
   if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
    return true;
  } else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) {
   if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
    return true;
  }
  return false;
 }

 /**
  * 产生周序列,即得到当前时间所在的年度是第几周
  * 
  * @return
  */
 public static String getSeqWeek() {
  Calendar c = Calendar.getInstance(Locale.CHINA);
  String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
  if (week.length() == 1)
   week = "0" + week;
  String year = Integer.toString(c.get(Calendar.YEAR));
  return year + week;
 }

 /**
  * 获得一个日期所在的周的星期几的日期,如要找出2002年2月3日所在周的星期一是几号
  * 
  * @param sdate
  * @param num
  * @return
  */
 public static String getWeek(String sdate, String num) {
  // 再转换为时间
  Date dd = VeDate.strToDate(sdate);
  Calendar c = Calendar.getInstance();
  c.setTime(dd);
  if (num.equals("1")) // 返回星期一所在的日期
   c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
  else if (num.equals("2")) // 返回星期二所在的日期
   c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
  else if (num.equals("3")) // 返回星期三所在的日期
   c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
  else if (num.equals("4")) // 返回星期四所在的日期
   c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
  else if (num.equals("5")) // 返回星期五所在的日期
   c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
  else if (num.equals("6")) // 返回星期六所在的日期
   c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
  else if (num.equals("0")) // 返回星期日所在的日期
   c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
  return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
 }

 /**
  * 根据一个日期,返回是星期几的字符串
  * 
  * @param sdate
  * @return
  */
 public static String getWeek(String sdate) {
  // 再转换为时间
  Date date = VeDate.strToDate(sdate);
  Calendar c = Calendar.getInstance();
  c.setTime(date);
  // int hour=c.get(Calendar.DAY_OF_WEEK);
  // hour中存的就是星期几了,其范围 1~7
  // 1=星期日 7=星期六,其他类推
  return new SimpleDateFormat("EEEE").format(c.getTime());
 }
 public static String getWeekStr(String sdate){
  String str = "";
  str = VeDate.getWeek(sdate);
  if("1".equals(str)){
   str = "星期日";
  }else if("2".equals(str)){
   str = "星期一";
  }else if("3".equals(str)){
   str = "星期二";
  }else if("4".equals(str)){
   str = "星期三";
  }else if("5".equals(str)){
   str = "星期四";
  }else if("6".equals(str)){
   str = "星期五";
  }else if("7".equals(str)){
   str = "星期六";
  }
  return str;
 }

 /**
  * 两个时间之间的天数
  * 
  * @param date1
  * @param date2
  * @return
  */
 public static long getDays(String date1, String date2) {
  if (date1 == null || date1.equals(""))
   return 0;
  if (date2 == null || date2.equals(""))
   return 0;
  // 转换为标准时间
  SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
  java.util.Date date = null;
  java.util.Date mydate = null;
  try {
   date = myFormatter.parse(date1);
   mydate = myFormatter.parse(date2);
  } catch (Exception e) {
  }
  long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
  return day;
 }

 /**
  * 形成如下的日历 , 根据传入的一个时间返回一个结构 星期日 星期一 星期二 星期三 星期四 星期五 星期六 下面是当月的各个时间
  * 此函数返回该日历第一行星期日所在的日期
  * 
  * @param sdate
  * @return
  */
 public static String getNowMonth(String sdate) {
  // 取该时间所在月的一号
  sdate = sdate.substring(0, 8) + "01";

  // 得到这个月的1号是星期几
  Date date = VeDate.strToDate(sdate);
  Calendar c = Calendar.getInstance();
  c.setTime(date);
  int u = c.get(Calendar.DAY_OF_WEEK);
  String newday = VeDate.getNextDay(sdate, (1 - u) + "");
  return newday;
 }

 /**
  * 取得数据库主键 生成格式为yyyymmddhhmmss+k位随机数
  * 
  * @param k
  *            表示是取几位随机数,可以自己定
  */

 public static String getNo(int k) {

  return getUserDate("yyyyMMddhhmmss") + getRandom(k);
 }

 /**
  * 返回一个随机数
  * 
  * @param i
  * @return
  */
 public static String getRandom(int i) {
  Random jjj = new Random();
  // int suiJiShu = jjj.nextInt(9);
  if (i == 0)
   return "";
  String jj = "";
  for (int k = 0; k < i; k++) {
   jj = jj + jjj.nextInt(9);
  }
  return jj;
 }

 /**
  * 
  * @param args
  */
 public static boolean RightDate(String date) {

  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  ;
  if (date == null)
   return false;
  if (date.length() > 10) {
   sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  } else {
   sdf = new SimpleDateFormat("yyyy-MM-dd");
  }
  try {
   sdf.parse(date);
  } catch (ParseException pe) {
   return false;
  }
  return true;
 }

 /***************************************************************************
  * //nd=1表示返回的值中包含年度 //yf=1表示返回的值中包含月份 //rq=1表示返回的值中包含日期 //format表示返回的格式 1
  * 以年月日中文返回 2 以横线-返回 // 3 以斜线/返回 4 以缩写不带其它符号形式返回 // 5 以点号.返回
  **************************************************************************/
 public static String getStringDateMonth(String sdate, String nd, String yf, String rq, String format) {
  Date currentTime = new Date();
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  String dateString = formatter.format(currentTime);
  String s_nd = dateString.substring(0, 4); // 年份
  String s_yf = dateString.substring(5, 7); // 月份
  String s_rq = dateString.substring(8, 10); // 日期
  String sreturn = "";
  roc.util.MyChar mc = new roc.util.MyChar();
  if (sdate == null || sdate.equals("") || !mc.Isdate(sdate)) { // 处理空值情况
   if (nd.equals("1")) {
    sreturn = s_nd;
    // 处理间隔符
    if (format.equals("1"))
     sreturn = sreturn + "年";
    else if (format.equals("2"))
     sreturn = sreturn + "-";
    else if (format.equals("3"))
     sreturn = sreturn + "/";
    else if (format.equals("5"))
     sreturn = sreturn + ".";
   }
   // 处理月份
   if (yf.equals("1")) {
    sreturn = sreturn + s_yf;
    if (format.equals("1"))
     sreturn = sreturn + "月";
    else if (format.equals("2"))
     sreturn = sreturn + "-";
    else if (format.equals("3"))
     sreturn = sreturn + "/";
    else if (format.equals("5"))
     sreturn = sreturn + ".";
   }
   // 处理日期
   if (rq.equals("1")) {
    sreturn = sreturn + s_rq;
    if (format.equals("1"))
     sreturn = sreturn + "日";
   }
  } else {
   // 不是空值,也是一个合法的日期值,则先将其转换为标准的时间格式
   sdate = roc.util.RocDate.getOKDate(sdate);
   s_nd = sdate.substring(0, 4); // 年份
   s_yf = sdate.substring(5, 7); // 月份
   s_rq = sdate.substring(8, 10); // 日期
   if (nd.equals("1")) {
    sreturn = s_nd;
    // 处理间隔符
    if (format.equals("1"))
     sreturn = sreturn + "年";
    else if (format.equals("2"))
     sreturn = sreturn + "-";
    else if (format.equals("3"))
     sreturn = sreturn + "/";
    else if (format.equals("5"))
     sreturn = sreturn + ".";
   }
   // 处理月份
   if (yf.equals("1")) {
    sreturn = sreturn + s_yf;
    if (format.equals("1"))
     sreturn = sreturn + "月";
    else if (format.equals("2"))
     sreturn = sreturn + "-";
    else if (format.equals("3"))
     sreturn = sreturn + "/";
    else if (format.equals("5"))
     sreturn = sreturn + ".";
   }
   // 处理日期
   if (rq.equals("1")) {
    sreturn = sreturn + s_rq;
    if (format.equals("1"))
     sreturn = sreturn + "日";
   }
  }
  return sreturn;
 }

 public static String getNextMonthDay(String sdate, int m) {
  sdate = getOKDate(sdate);
  int year = Integer.parseInt(sdate.substring(0, 4));
  int month = Integer.parseInt(sdate.substring(5, 7));
  month = month + m;
  if (month < 0) {
   month = month + 12;
   year = year - 1;
  } else if (month > 12) {
   month = month - 12;
   year = year + 1;
  }
  String smonth = "";
  if (month < 10)
   smonth = "0" + month;
  else
   smonth = "" + month;
  return year + "-" + smonth + "-10";
 }

 public static String getOKDate(String sdate) {
  if (sdate == null || sdate.equals(""))
   return getStringDateShort();

  if (!VeStr.Isdate(sdate)) {
   sdate = getStringDateShort();
  }
  // 将“/”转换为“-”
  sdate = VeStr.Replace(sdate, "/", "-");
  // 如果只有8位长度,则要进行转换
  if (sdate.length() == 8)
   sdate = sdate.substring(0, 4) + "-" + sdate.substring(4, 6) + "-" + sdate.substring(6, 8);
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  ParsePosition pos = new ParsePosition(0);
  Date strtodate = formatter.parse(sdate, pos);
  String dateString = formatter.format(strtodate);
  return dateString;
 }

 public static void main(String[] args) throws Exception {
  try {
   //System.out.print(Integer.valueOf(getTwoDay("2006-11-03 12:22:10", "2006-11-02 11:22:09")));
  } catch (Exception e) {
   throw new Exception();
  }
  //System.out.println("sss");
 }

分类: Core Java

好文要顶 关注我 收藏该文  

EdwardLau
关注 - 0
粉丝 - 98

+加关注

14

0

« 上一篇: 我的程序员之路(一) 
» 下一篇: 研究:Liferay研究(二)开发Liferay中几个常用的标签

posted @ 2010-03-21 14:25 EdwardLau 阅读(632616) 评论(1) 编辑 收藏

评论列表

  

#1楼 2013-06-10 11:24 Chunwai Yeung

经测试
/**
* 获取现在时间
* 
* @return返回短时间格式 yyyy-MM-dd
*/
返回为null

支持(3) 反对(0)

刷新评论刷新页面返回顶部

注册用户登录后才能发表评论,请 登录 或 注册, 访问 网站首页。

【推荐】超50万行VC++源码: 大型组态工控、电力仿真CAD与GIS源码库
【活动】京东云服务器_云主机低于1折,低价高性能产品备战双11
【推荐】天翼云双十一提前开抢,1核1G云主机3个月仅需59元
【推荐】阿里云双11冰点钜惠,热门产品低至一折等你来抢!
【福利】个推四大热门移动开发SDK全部免费用一年,限时抢!

 

相关博文:
· Java时间日期格式转换
· Java时间日期格式转换
· Java时间日期格式转换
· Java时间日期格式转换
· java日期时间格式转换
» 更多推荐...

 

最新 IT 新闻: 
· SpaceX拟11日发射第二批星链卫星 还用上二手整流罩 
· Facebook又爆隐私丑闻:100多第三方开发者违规访问用户数据 
· 金山办公:发行价确定为45.86元/股 
· 美“天鹅座”货运飞船抵达空间站 
· 欧盟文件敦促发行公共数字货币 可能封杀Facebook加密货币 
» 更多新闻...

公告

昵称: EdwardLau 
园龄: 8年10个月 
粉丝: 98 
关注: 0

+加关注

<2010年3月>
日一二三四五六
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910

搜索

 

 

常用链接

我的标签

随笔分类 (125)

随笔档案 (125)

最新评论

阅读排行榜

评论排行榜

推荐排行榜

医学图像分割评价指标详解:从Dice到MSD的实战解析 本文深入解析医学图像分割的核心评价指标,从最常用的Dice相似系数到衡量边界精度的MSD(最大对称表面距离),通过实战案例与代码演示,系统阐述各指标的计算原理、应用场景及临床意义,帮助读者全面评估模型性能。 阅读详情

相关推荐

宝骏kiwi 630 730 560 E100 E200 E300 310 510 610 330 530维修手册电路图资料线路图接线图更新

2020-2022年宝骏RS-3维修手册和电路图。2020-2022年宝骏RS-3维修手册和电路图。2019-2021年宝骏RC-6维修手册和电路图。2019-2021年宝骏RC-6维修手册和电路图。2019-2020年宝骏RS-5维修手册和电路图。2019-2020年宝骏RS-5维修手册和电路图。2019-2020年宝骏RM-5维修手册和电路图。2019-2020年宝骏RM-5维修手册和电路图。2019-2021年宝骏510维修手册和电路图。2019-2021年宝骏510维修手册和电路图。

weixin_45821055的博客 1778

java中文数字日期转为阿拉伯数字日期

String[] chineseDigits = {"〇", "一", "二", "三", "四", "五", "六", "七", "八", "九"};请注意,这只是一个简单的示例代码,仅适用于简单的中文数字日期转换。如果需要更复杂的转换,可能需要使用更高级的技术,如自然语言处理(NLP)或正则表达式匹配。,分别表示中文数字和对应的阿拉伯数字。然后,通过循环遍历将中文日期字符串中的中文数字替换为对应的阿拉伯数字。Java本身没有内置直接将中文数字日期转换为阿拉伯数字日期的库。

u010479989的博客 1297

别再乱删文件了!修复Print Spooler服务自动停止的3个关键检查点(附Win10/Win11实测)

本文深入解析Windows系统中Print Spooler服务自动停止的常见原因及修复方法。从第三方驱动冲突、权限与服务依赖关系到打印队列积压,提供3个关键检查点的详细排查步骤,帮助用户彻底解决打印服务异常问题,适用于Win10/Win11系统。

weixin_31569671的博客 702

java将日期格式字符串2015-08-05T09:40:00+08:00转换为yyyy-MM-dd HH:mm:ss

1.该方法jdk1.7以上才支持 /** * 将格林威治时间字符串转换为yyyy-MM-dd HH:mm:ss字符串,JDK1.7以上版本支持该方法 * @param s * @return */ public static String DateString2formatString(String s) { String

asuyunlong的专栏 2万+

保姆级教程:用STM8S207R6和FD6288T自制BLDC驱动板,从原理图到代码框架搭建

本文提供了一份详细的STM8S207R6和FD6288T自制BLDC驱动板教程,涵盖从原理图设计到代码框架搭建的全过程。通过硬件选型、电源电路设计、PWM定时器配置等关键步骤解析,帮助开发者快速掌握无刷直流电机驱动技术,实现高效稳定的电机控制方案。

weixin_28729205的博客 496

ffmpeg 4.4 cenc-aes-ctr 加解密 MP4 工程性质分析

ffmpeg 4.4 cenc-aes-ctr 加解密 MP4 工程性质分析

Dream_Dynasty的博客 3059

玩转Java时间格式转换(亲测可用)

相信很多小伙伴在时间格式转换的时候非常头疼,我也是如此,所以总结以下的方法供大家使用,都是我自己用过没问题的大家开箱即用!

Sunshineづ 1万+

Mongodb操作与Java(四)MongoTemplate的使用

MongoTemplate的使用

A乐神的博客 3441

java如何处理2016-09-03T00:00:00.000+08:00时间格式

public class DealDateFormatUtil { public static String dealDate(String oldDateStr) throws ParseException { DateFormat df = new SimpleDateFormat(&quot;yyyy-MM-dd'T'HH:mm:ss&quot;); Date date = df.parse(o...

weixin_38538232的博客 1万+

Winform中使用EasyPlayer-RTSP-Win开源组件实现播放RTSP视频流

场景 开源RTMP组件EasyPusher-Android+EasyDarwin实现APP推流给RTSP流媒体服务器: 开源RTMP组件EasyPusher-Android+EasyDarwin实现APP推流给RTSP流媒体服务器_BADAO_LIUMANG_QIZHI的博客-CSDN博客 上面介绍了RTSP相关的开源组件,如果要在Windows中实现对RTSP视频流的播放, 可以使用开源组件EasyPlayer-RTSP-Win GitHub - tsingsee/EasyPlayer-RTSP

BADAO_LIUMANG_QIZHI的博客 1万+

点云从入门到精通技术详解100篇-基于点云语义分割的电力线提取及聚类研究

在现代社会,电力一直扮演着最重要的角色之一。它不仅与人民的生活息息相关,更是一个国家发展的命脉。近年来,随着我国各特高压输电线路建成(比如2022年1月份白鹤滩江苏特高压输电线路的放线)以及电网规模的不断扩大,我国年发电量稳步上升,人民的生活水平越来越高。我国土地辽阔,输电线路分布广泛,再加上地形环境复杂多样,电力线可能遭受风吹雨晒甚至是冰雪寒灾的影响。因此,为了保障电力线的可靠供电,防止停电意外发生,电力部门要定期对输电线路进行检查。目前我国主要。

getusushu的博客 1012

日期格式转换 java 2016-09-03T00:00:00.000+08:00

/** * 日期格式转换yyyy-MM-dd'T'HH:mm:ss.SSSXXX (yyyy-MM-dd'T'HH:mm:ss.SSSZ)TO yyyy-MM-dd HH:mm:ss * @throws ParseException */public static String dealDateFormat(String oldDateStr) throws ParseExcept...

weixin_34357928的博客 3410

【ArcGIS】07 ArcHydroTools对COM组件的调用返回了错误

在用Arc Hydro Tools提取流域时,在DEM Reconditioning时可能会遇到这个问题: 经过摸索,发现了问题产生的原因: 结果文件保存在了有中文的路径中。 解决的办法: 首先,把ArcGIS切换成英文版 搜索ArcGlS Administrator,以管理员方式打开 点击高级,选择语言为English 其次,新建mxd文档 删除原来的mxd文档,新建mxd文档,放置原来的图层文件。如果不这样做,最后一步的Map Name还会是“图层”这两个汉字。 最后,设置Arc Hydro T

记录专业模型开发、算法程序设计、软件学习笔记、生活日志。 4925

SDUT - 2246 时间日期格式转换 (Java)

时间日期格式转换Time Limit: 1000 ms Memory Limit: 65536 KiBSubmit StatisticProblem Description而北美所用的日期格式则为“月月/日日/年年年年”或”mm/dd /yyyy”,如将“2010/11/20”改成这种格式,对应的则是”11/20/2010”。对于时间的格式,则常有12小时制和24小时制12小时制的表示方法是”05...

Euthanazia的博客 511

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

Java 时间日期格式转换

时间日期格式转换Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description而北美所用的日期格式则为“月月/日日/年年年年”或”mm/dd /yyyy”,如将“2010/11/20”改成这种格式,对应的则是”11/20/2010”。对于时间的格式,则常有12...

Joywss的博客 1300

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

Java—时间日期格式转换

Java—时间日期格式转换 Description 对于日期的常用格式,在中国常采用格式的是“年年年年/月月/日日”或写为英语缩略表示的”yyyy/mm/dd”,此次编程竞赛的启动日期“2010/11/20”就是符合这种格式的一个日期, 而北美所用的日期格式则为“月月/日日/年年年年”或”mm/dd /yyyy”,如将“2010/11/20”改成这种格式,对应的则是”11/20/2010”。对于时间的格式,则常有12小时制和24小时制 的表示方法,24小时制用0-24来表示一天中的24小时,而12小时制只采

qq_45885219的博客 1077

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

Java sdut ACM 2246 时间日期格式转换

时间日期格式转换 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 而北美所用的日期格式则为“月月/日日/年年年年”或”mm/dd /yyyy”,如将“2010/11/20”改成这种格式,对应的则是”11/20/2010”。对于时间的格式,则常有12小时制和24小时制

公子世无双的博客 577

Java sdut acm 2246 时间日期格式转换

题目链接:点击打开链接 时间日期格式转换 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 对于日期的常用格式,在中国常采用格式的是“年年年年/月月/日日”或写为英语缩略表示的”yyyy/mm/dd”,此次编程竞赛的启动日期“2010/11/20”就是符合这种格

Annfan123的博客 1182

Java中时间日期格式转换

参见一篇博文 后续项目会继续深入研究

博客写得好,技术好,才是真的好 625
上一篇: producer参数的理解
下一篇: 线程池中shutdown,awaitTermination,shutdownNow的作用和区别
王树民
博客等级 码龄10年 912粉丝 560原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址