「javagmt转换」java字节转换成M
今天给各位分享javagmt转换的知识,其中也会对java字节转换成M进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、java如何将GMT格式时间字符串转换为java.util.Date对象
- 2、java时间格式转换 Thu Dec 24 17:33:00 GMT+08:00 2015 我想转
- 3、java中怎么将日期转换为GMT格式
- 4、如何使用java将GMT转换为UTC格式
- 5、java Thu Sep 04 2014 00:00:00 GMT 0800转换成年月日
- 6、Java如何根据TimeZone转换时间,可以给出相关的例子吗?谢谢
java如何将GMT格式时间字符串转换为java.util.Date对象
public Date getDate(String time) throws ParseException{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = sdf.parse(time);
return date;
}
java时间格式转换 Thu Dec 24 17:33:00 GMT+08:00 2015 我想转
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
String str=df.format(date);
java中怎么将日期转换为GMT格式
其实不管建不建议,能用就行,不非得用推荐的。
String toGMT(Date date) {
try {
SimpleDateFormat format = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance(new SimpleTimeZone(0, "GMT"));
format.setCalendar(cal);
return format.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
}
如何使用java将GMT转换为UTC格式
String toGMT(Date date) {
try {
SimpleDateFormat format = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance(new SimpleTimeZone(0, "GMT"));
format.setCalendar(cal);
return format.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
}
java Thu Sep 04 2014 00:00:00 GMT 0800转换成年月日
//如果是Object类型的时间,可以这么搞,如果是字符串的时间,直接字符串处理(此处略)
var date = new Date(value.time);//value是一个日期的Object
//格式转为 yyyy-mm-dd
date=date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();
Java如何根据TimeZone转换时间,可以给出相关的例子吗?谢谢
通过TimeZone的getTimeZone方法来实现,具体可参考下面代码例子:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
publicclass CalendarTime {
publicstaticfinal String DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";
privatestatic TimeZone USER_TIMEZONE = TimeZone.getTimeZone("PRC");
privatestatic TimeZone GMT_TIMEZONE = TimeZone.getTimeZone("GMT");
publicstaticvoid main(String[] args) throws ParseException {
System.out.println("start test---------------------");
// construct a string to indicate a local time
String testTime = new String("2008-02-02 08:30:33");
System.out.println("convert a local string to a local gmt time");
System.out.println(testTime);
System.out.println("convert to GMT time");
// convert the string to a gmt calendar
Calendar gmtCal = CalendarTime
.convertLocalDateStringToGMTCalendar(testTime);
System.out.println(getDateFormat(GMT_TIMEZONE).format(gmtCal.getTime())
+ " " + gmtCal.getTimeZone().getID());
Calendar localCal = convertGMTToCNTime(gmtCal);
System.out.println(localCal.getTime());
System.out.println("convert a gmt calendar to a local string");
Calendar localTime = Calendar.getInstance();
localTime.set(2008, 01, 02, 0, 30, 33);
System.out.println(localTime.getTime());
Calendar gmtTime = convertCNTimeToGMT(localTime);
System.out.println(getDateFormat(GMT_TIMEZONE)
.format(gmtTime.getTime())
+ " " + gmtTime.getTimeZone().getID());
String localStr = convertToLocalTimeString(gmtTime);
System.out.println(localStr);
}
/**
*getadateformatforanytimezone
*
*@paramtimeZone
*@return
*/
publicstatic SimpleDateFormat getDateFormat(TimeZone timeZone) {
SimpleDateFormat formater = new SimpleDateFormat(DATE_PATTERN);
formater.setTimeZone(timeZone);
return formater;
}
/**
*convertacalendartoanylocaltimestring
*
*@paramcalendar
*@return
*/
publicstatic String convertToLocalTimeString(Calendar calendar) {
if (null == calendar) {
returnnull;
}
return getDateFormat(USER_TIMEZONE).format(calendar.getTime());
}
/**
*convertanydatestringtoagmtcalendar
*
*@paramtime
*@return
*@throwsParseException
*/
publicstatic Calendar convertLocalDateStringToGMTCalendar(String time)
throws ParseException {
Date date = getDateFormat(USER_TIMEZONE).parse(time.trim());
Calendar calendar = new GregorianCalendar(GMT_TIMEZONE);
calendar.setTime(date);
return calendar;
}
/**
*convertagmtcalendartoalocalcalendarjustmodifyitstimezonebut
*willnotchangeitstime
*
*@paramtime
*@return
*/
publicstatic Calendar convertGMTToCNTime(Calendar time) {
time.setTimeZone(GMT_TIMEZONE);
Calendar cnTime = Calendar.getInstance();
int begin_year = time.get(Calendar.YEAR);
int begin_month = time.get(Calendar.MONTH);
int begin_day = time.get(Calendar.DAY_OF_MONTH);
int begin_hour = time.get(Calendar.HOUR_OF_DAY);
int begin_minute = time.get(Calendar.MINUTE);
int begin_second = time.get(Calendar.SECOND);
cnTime.set(Calendar.YEAR, begin_year);
cnTime.set(Calendar.MONTH, begin_month);
cnTime.set(Calendar.DAY_OF_MONTH, begin_day);
cnTime.set(Calendar.HOUR_OF_DAY, begin_hour);
cnTime.set(Calendar.MINUTE, begin_minute);
cnTime.set(Calendar.SECOND, begin_second);
return cnTime;
}
/**
*convertalocalcalendartoagmtcalendaronlychangeitstimezonebut
*willnotchangeitstime
*
*@paramcnTime
*@return
*/
publicstatic Calendar convertCNTimeToGMT(Calendar cnTime) {
Calendar result = new GregorianCalendar(GMT_TIMEZONE);
int year = cnTime.get(Calendar.YEAR);
int month = cnTime.get(Calendar.MONTH);
int day = cnTime.get(Calendar.DAY_OF_MONTH);
int hour = cnTime.get(Calendar.HOUR_OF_DAY);
int minute = cnTime.get(Calendar.MINUTE);
int second = cnTime.get(Calendar.SECOND);
result.set(Calendar.YEAR, year);
result.set(Calendar.MONTH, month);
result.set(Calendar.DAY_OF_MONTH, day);
result.set(Calendar.HOUR_OF_DAY, hour);
result.set(Calendar.MINUTE, minute);
result.set(Calendar.SECOND, second);
return result;
}
}
start test---------------------
convert a local string to a local gmt time
2008-02-02 08:30:33
convert to GMT time
2008-02-02 00:30:33 GMT
Sat Feb 02 00:30:33 GMT+08:00 2008
convert a gmt calendar to a local string
Sat Feb 02 00:30:33 GMT+08:00 2008
2008-02-02 00:30:33 GMT
2008-02-02 08:30:33
关于javagmt转换和java字节转换成M的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-11-22,除非注明,否则均为
原创文章,转载请注明出处。