
你这个就是当前时间和你说的电脑显示时间两个是一样的= ="
CalendargetInstance()getTime()<----系统时间
抓网路时间的方法 大陆区请自行换抓取网址
/
网路时间
@return
/
public String date() {
String date = null;
try {
URL url = new URL(">
首先获取当前时间:
javautilDate nowdate = new javautilDate();
2/2
然后如果你想时间的格式和你想用的时间格式一致 那么就要格式化时间了SimpleDateFormat 的包在javatext包下SimpleDateFormat
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") //年月日 时分秒
String t = sdfparse(nowdate);
import javatextSimpleDateFormat;
import javautilDate;
Date d=new Date();//获取时间
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//转换格式
Systemoutprintln(sdfformat(d));//打印
package util;
import javamathBigDecimal;
import javatextParseException;
import javatextSimpleDateFormat;
import javautilCalendar;
import javautilDate;
/
获取系统时间
/
public class DateUtil {
/ 日志对象 /
// private static Logger logger = LoggergetLogger(SystemUtilclass);
/ 获取年份 /
public static final int YEAR = 1;
/ 获取年月 /
public static final int YEARMONTH = 2;
/ 获取年月日 /
public static final int YEARMONTHDAY = 3;
/ 获取年月日,小时 /
public static final int YMD_HOUR = 4;
/ 获取年月日,小时,分钟 /
public static final int YMD_HOURMINUTE = 5;
/ 获取年月日,时分秒 /
public static final int FULL = 6;
/ 获取年月日时分秒 格式:yyyyMMddHHmmss /
public static final int UTILTIME = 7;
/
根据指定时间格式类型得到当前时间
@param type
时间类型
@return String 字符串时间
/
public static synchronized String getCurrentTime(int type) {
String format = getFormat(type);
SimpleDateFormat timeformat = new SimpleDateFormat(format);
Date date = new Date();
return timeformatformat(date);
}
/
返回当前系统时间的年月日
@return
/
public static synchronized String getCurrentTime() {
SimpleDateFormat timeformat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
return timeformatformat(date);
}
/
根据参数格式,格式化当前日期
@param format
@return
/
public static synchronized String getDateString(String format) {
SimpleDateFormat timeformat = new SimpleDateFormat(format);
Date date = new Date();
return timeformatformat(date);
}
/
根据指定时间格式类型,格式化时间格式
@param type
时间格式类型
@return
/
private static String getFormat(int type) {
String format = "";
if (type == 1) {
format = "yyyy";
} else if (type == 2) {
format = "yyyy-MM";
} else if (type == 3) {
format = "yyyy-MM-dd";
} else if (type == 4) {
format = "yyyy-MM-dd HH";
} else if (type == 5) {
format = "yyyy-MM-dd HH:mm";
} else if (type == 6) {
format = "yyyy-MM-dd HH:mm:ss";
} else if (type == 7) {
format = "yyyyMMddHHmmss";
} else {
throw new RuntimeException("日期格式参数错误");
}
return format;
}
public static int getYear(String dateString) {
SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = ddparse(dateString);
Calendar cal = CalendargetInstance();
calsetTime(date);
return calget(CalendarYEAR);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static int getMonth(String dateString) {
SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = ddparse(dateString);
Calendar cal = CalendargetInstance();
calsetTime(date);
return calget(CalendarMONTH)+1;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static int getDay(String dateString) {
SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = ddparse(dateString);
Calendar cal = CalendargetInstance();
calsetTime(date);
return calget(CalendarDAY_OF_MONTH);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static Date StringToDate(String dateStr, String formatStr) {
SimpleDateFormat dd = new SimpleDateFormat(formatStr);
Date date = null;
try {
date = ddparse(dateStr);
} catch (ParseException e) {
eprintStackTrace();
}
return date;
}
/
当前日期和参数日期距离的小时数 日期格式:yyyy-MM-dd HH:mm:ss
@param date
@return
/
public static double getHours(String date) {
SimpleDateFormat timeformat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
try {
Date d = new Date();
Date d1 = timeformatparse(date);
long temp = dgetTime() - d1getTime();
double f = temp / 3600000d;
BigDecimal b = new BigDecimal(f);
double f1 = bsetScale(2, BigDecimalROUND_HALF_UP)doubleValue();
return f1;
} catch (Exception e) {
eprintStackTrace();
throw new RuntimeException(e);
}
}
public static void main(String a[]) {
try {
int aa = getYear("2012-01-08");
Systemoutprintln(aa);
} catch (Exception e) {
eprintStackTrace();
}
}
}
以上就是关于java怎么获取当前时间 不是电脑显示的时间全部的内容,包括:java怎么获取当前时间 不是电脑显示的时间、java 如何获取当前时间的时间戳、java怎么获取当前系统时间 毫秒数等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)