Java时间日期格式转换

Win11显卡驱动安装失败的全面解决方案 Windows 11显卡驱动安装失败是一个比较常见的问题,但通过上述方法,我们可以有效地解决这一问题。方法一通过调整组策略编辑器的驱动程序安装策略,可能解决一些因签名问题导致的安装失败;而方法二则通过重装Windows 11系统,为系统提供一个全新的、干净的环境,从而可能解决更复杂的安装问题。在实际操作中,用户可以根据具体情况选择合适的方法来解决显卡驱动安装失败的问题。 阅读详情

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)

最新评论

阅读排行榜

评论排行榜

推荐排行榜

从AI画稿到3D虚拟时装:Illustrator与Substance 3D的服装设计工作流 本文旨在解决传统服装与纺织品设计师在产品开发流程中,面临的“2D印花图案无法直观预见3D上身效果”、“物理打样成本高昂且周期漫长”的核心痛点。我们将介绍一套纯数字化的3D可视化工作流,核心是利用 Adobe Illustrator 进行矢量印花图案创作,通过 Adobe Substance 3D Sampler 将其转化为逼真的PBR(基于物理渲染)布料材质,并最终在 Adobe Substance 3D Stager 中,将该材质应用于3D服装模型,渲染出照片级的虚拟样衣。本指南将帮助设计师以极低的成本和 阅读详情

相关推荐

Android HCE模拟NFC标签:实现跨设备NDEF数据共享

本文详细介绍了Android HCE技术如何模拟NFC标签实现跨设备NDEF数据共享。通过核心代码实现和标准AID配置,使手机能够被第三方NFC应用直接读取,支持文本、网址、Wi-Fi配置等多种数据类型,大幅提升设备间数据交换的便捷性和兼容性。

pytorch8learner的博客 248

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

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

u010479989的博客 1297

硬件工程师入门进阶,一文搞懂,看这篇就对了

每家公司的datasheet都有严格的规范,及独特的风格,有时候还会穿插一些原理性的介绍,比如ADI公司的一些ADC/DAC器件资料里面,还会包含AD/DA转换的原理性讲解内容,Datasheet既可以是我们设计上的好帮手,也是学习的好资料。在出货量大的产品中,尽量减少物料种类,比如开头说的,1%的电阻不得不用,那么5%的替换成1%的,物料种类就减少一种,量就大了,能不能争取更优的价格呢?如果说前面学的是内功,那这一关,是招式、是工具的使用,原理图和PCB设计都有对应的工具,教程也很多,且比较容易上手。

一路带飞的博客 5万+

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

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

Sunshineづ 1万+

auto.js梦幻西游游戏脚本,梦幻西游脚本开发思路

本篇文章给大家谈谈梦幻西游脚本用什么语言写,以及auto.js梦幻西游游戏脚本,希望对各位有所帮助,不要忘了收藏本站喔。

npy199331的博客 1939

LPDDR6与LPDDR5 channel/rank/die/bank技术探讨

LPDDR6与LPDDR5 channel/rank/die/bank技术探讨

zilan23的博客 3641

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万+

手把手教你学Simulink——基于传统电网建模与稳定性分析的场景实例:基于同步发电机的单机无穷大系统建模与稳定性分析

本文详细介绍了使用MATLAB/Simulink进行单机无穷大系统(SMIB)建模与稳定性分析的方法。教程从系统结构设计入手,包含同步发电机、输电线路、无穷大母线等核心模块的建模步骤和参数配置示例。重点讲解了七个关键步骤:项目创建、模块配置、控制系统集成、负载添加(可选)以及仿真运行与结果分析,并提供了具体的MATLAB代码示例。最后通过性能评估指标(功角差、电压跌落、频率偏差)分析系统稳定性,为电力系统动态特性研究提供了实用指导,强调准确参数设置对仿真结果的重要性。

MHD0815的博客 298

JAVA后台时间各种格式相互转换

public class DateUtil { private static final Log log = LogFactory.getLog(DateUtil.class); /** 年月日 时分秒模式字符串 */ public static final String YEAR_MONTH_DAY_HOUR_MINUTE_SECOND_PATTERN = "yyyy-MM-dd HH:

zhibu_8048的博客 2575

风电功率预测项目技术白皮书:短期、超短期、中长期全尺度算法详解与工程落地

聚焦风电功率预测的时间尺度与算法:短期(0~72h)以XGBoost/LightGBM/CatBoost及深度学习为主,超短期(0~4h)采用ARMA+LSTM,中长期用LightGBM等,并涵盖概率预测、多气象源融合、AI大模型+物理模型混合策略、极端天气应对及工程运维要点。

算法工程师 323

hwasan实例分析踩内存之一:camx进程stack tag-mismatch的三种解决方案

hwasan实例分析踩内存之一:camx进程stack tag-mismatch的三种解决方案。

1291

2021-09-09 Java中的日期时间 - yahya

JDK8

yahya·小阳的博客 1264

(LDA新闻文本主题分析)基于 gensim训练LDA模型,进行新闻文本主题分析 完整代码+数据 可直接运行

(LDA新闻文本主题分析)基于 gensim训练LDA模型,进行新闻文本主题分析 完整代码+数据 可直接运行

机器学习深度学习业余选手 1498

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的博客 510

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

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

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

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中时间日期格式转换

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

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

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值