获取系统当前时间:

时间:2021-02-19 22:43:11

获取系统当前时间:

  Java代码

  public static String getSystemTime(){

  Date date=new Date();

  SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

  return df.format(date);

  }

  //字符串转化成时间类型(字符串可以是任意类型,只要和SimpleDateFormat中的格式一致即可)

  java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("M/dd/yyyy hh:mm:ss a",java.util.Locale.US);

  java.util.Date d = sdf.parse("5/13/2003 10:31:37 AM");

  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

  String mDateTime1=formatter.format(d);

  //当前时间

  Calendar cal = Calendar.getInstance();

  // SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss G E D F w W a E F");

  String mDateTime=formatter.format(cal.getTime());

  //1年前日期

  java.util.Date myDate=new java.util.Date();

  long myTime=(myDate.getTime()/1000)-60*60*24*365;

  myDate.setTime(myTime*1000);

  String mDate=formatter.format(myDate);

  //明天日期

  myDate=new java.util.Date();

  myTime=(myDate.getTime()/1000)+60*60*24;

  myDate.setTime(myTime*1000);

  mDate=formatter.format(myDate);

  //两个时间之间的天数

  SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");

  java.util.Date date= myFormatter.parse("2003-05-1");

  java.util.Date mydate= myFormatter.parse("1899-12-30");

  long day=(date.getTime()-mydate.getTime())/(24*60*60*1000);

  //加半小时

  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

  java.util.Date date1 = format.parse("2002-02-28 23:16:00");

  long Time=(date1.getTime()/1000)+60*30;

  date1.setTime(Time*1000);

  String mydate1=formatter.format(date1);

  //年月周求日期

  SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM F E");

  java.util.Date date2= formatter2.parse("2003-05 5 星期五");

  SimpleDateFormat formatter3 = new SimpleDateFormat("yyyy-MM-dd");

  String mydate2=formatter3.format(date2);

  //求是星期几

  mydate= myFormatter.parse("2001-1-1");

  SimpleDateFormat formatter4 = new SimpleDateFormat("E");

  String mydate3=formatter4.format(mydate);

  }

  在 开发web应用中,针对不同的数据库日期类型,我们需要在我们的程序中对日期类型做各种不同的转换。若对应数据库数据是oracle的Date类型,即只 需要年月日的,可以选择使用java.sql.Date类型,若对应的是MSsqlserver数据库的DateTime类型,即需要年月日时分秒的,选 择java.sql.Timestamp类型

  你可以使用dateFormat定义时间日期的格式,转一个字符串即可

  Java代码

  package personal.jessica;

  import java.util.Date;

  import java.util.Calendar;

  import java.sql.Timestamp;

  import java.text.DateFormat;

  import java.text.SimpleDateFormat;

  import java.util.Locale;

  class Datetest{

  /**

  *method 将字符串类型的日期转换为一个timestamp(时间戳记java.sql.Timestamp)

  *@param dateString 需要转换为timestamp的字符串

  *@return dataTime timestamp

  */

  public final static java.sql.Timestamp string2Time(String dateString)

  throws java.text.ParseException {

  DateFormat dateFormat;

  dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss.SSS", Locale.ENGLISH);//设定格式

  //dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss", Locale.ENGLISH);

  dateFormat.setLenient(false);

  java.util.Date timeDate = dateFormat.parse(dateString);//util类型

  java.sql.Timestamp dateTime = new java.sql.Timestamp(timeDate.getTime());//Timestamp类型,timeDate.getTime()返回一个long型

  return dateTime;

  }