Pat*_*man 82 java android android-date java-date
我有几毫秒.我需要它转换为日期格式
例:
23/10/2011
怎么实现呢?
Utt*_*tam 183
试试这个示例代码: -
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Test {
/**
* Main Method
*/
public static void main(String[] args) {
System.out.println(getDate(82233213123L, "dd/MM/yyyy hh:mm:ss.SSS"));
}
/**
* Return date in specified format.
* @param milliSeconds Date in milliseconds
* @param dateFormat Date format
* @return String representing date in specified format
*/
public static String getDate(long milliSeconds, String dateFormat)
{
// Create a DateFormatter object for displaying date in specified format.
SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
// Create a calendar object that will convert the date and time value in milliseconds to date.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
return formatter.format(calendar.getTime());
}
}
Run Code Online (Sandbox Code Playgroud)
我希望这有帮助......
Pri*_*ley 71
将毫秒值转换为Date实例并将其传递给选择的格式化程序.
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateString = formatter.format(new Date(dateInMillis)));
Run Code Online (Sandbox Code Playgroud)
Ati*_*ood 33
public static String convertDate(String dateInMilliseconds,String dateFormat) {
return DateFormat.format(dateFormat, Long.parseLong(dateInMilliseconds)).toString();
}
Run Code Online (Sandbox Code Playgroud)
调用此功能
convertDate("82233213123","dd/MM/yyyy hh:mm:ss");
Run Code Online (Sandbox Code Playgroud)
ing*_*abh 10
尝试此代码可能会有所帮助,修改它适合您的需求
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date d = format.parse(fileDate);
Run Code Online (Sandbox Code Playgroud)
我终于找到适合我的普通代码
Long longDate = Long.valueOf(date);
Calendar cal = Calendar.getInstance();
int offset = cal.getTimeZone().getOffset(cal.getTimeInMillis());
Date da = new Date();
da = new Date(longDate-(long)offset);
cal.setTime(da);
String time =cal.getTime().toLocaleString();
//this is full string
time = DateFormat.getTimeInstance(DateFormat.MEDIUM).format(da);
//this is only time
time = DateFormat.getDateInstance(DateFormat.MEDIUM).format(da);
//this is only date
Run Code Online (Sandbox Code Playgroud)
简短有效:
DateFormat.getDateTimeInstance().format(new Date(myMillisValue))
Run Code Online (Sandbox Code Playgroud)
Instant.ofEpochMilli( myMillisSinceEpoch ) // Convert count-of-milliseconds-since-epoch into a date-time in UTC (`Instant`).
.atZone( ZoneId.of( "Africa/Tunis" ) ) // Adjust into the wall-clock time used by the people of a particular region (a time zone). Produces a `ZonedDateTime` object.
.toLocalDate() // Extract the date-only value (a `LocalDate` object) from the `ZonedDateTime` object, without time-of-day and without time zone.
.format( // Generate a string to textually represent the date value.
DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) // Specify a formatting pattern. Tip: Consider using `DateTimeFormatter.ofLocalized…` instead to soft-code the formatting pattern.
) // Returns a `String` object.
Run Code Online (Sandbox Code Playgroud)
现代方法使用java.time类来代替所有其他Answers使用的麻烦的旧旧日期时间类。
假设long自1970年1月的UTC 1970-01-01T00:00:00Z的纪元参考起有毫秒数。
Instant instant = Instant.ofEpochMilli( myMillisSinceEpoch ) ;
Run Code Online (Sandbox Code Playgroud)
要获取日期需要一个时区。在任何给定时刻,日期都会在全球范围内变化。
ZoneId z = ZoneId.of( "Pacific/Auckland" ) ;
ZonedDateTime zdt = instant.atZone( z ) ; // Same moment, different wall-clock time.
Run Code Online (Sandbox Code Playgroud)
提取仅日期的值。
LocalDate ld = zdt.toLocalDate() ;
Run Code Online (Sandbox Code Playgroud)
使用标准ISO 8601格式生成表示该值的字符串。
String output = ld.toString() ;
Run Code Online (Sandbox Code Playgroud)
生成自定义格式的字符串。
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;
String output = ld.format( f ) ;
Run Code Online (Sandbox Code Playgroud)
提示:考虑让java.time为您自动本地化,而不是硬编码格式模式。使用DateTimeFormatter.ofLocalized…方法。
该java.time框架是建立在Java 8和更高版本。这些类取代麻烦的老传统日期时间类,如java.util.Date,Calendar,和SimpleDateFormat。
现在处于维护模式的Joda-Time项目建议迁移到java.time类。
要了解更多信息,请参见Oracle教程。并在Stack Overflow中搜索许多示例和说明。规格为JSR 310。
您可以直接与数据库交换java.time对象。使用与JDBC 4.2或更高版本兼容的JDBC驱动程序。不需要字符串,不需要类。java.sql.*
在哪里获取java.time类?
该ThreeTen-额外项目与其他类扩展java.time。该项目为将来可能在java.time中添加内容提供了一个试验场。你可能在这里找到一些有用的类,比如Interval,YearWeek,YearQuarter,和更多。
在 Android 中将纪元格式转换为 SimpleDateFormat (Java / Kotlin)
输入:1613316655000
输出:2021-02-14T15:30:55.726Z
爪哇语
long milliseconds = 1613316655000L;
Date date = new Date(milliseconds);
String mobileDateTime = Utils.getFormatTimeWithTZ(date);
Run Code Online (Sandbox Code Playgroud)
//以字符串形式返回 SimpleDateFormat 的方法
public static String getFormatTimeWithTZ(Date currentTime) {
SimpleDateFormat timeZoneDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault());
return timeZoneString = timeZoneDate.format(currentTime);
}
Run Code Online (Sandbox Code Playgroud)
在科特林中
var milliseconds = 1613316655000L
var date = Date(milliseconds)
var mobileDateTime = Utils.getFormatTimeWithTZ(date)
Run Code Online (Sandbox Code Playgroud)
//以字符串形式返回 SimpleDateFormat 的方法
fun getFormatTimeWithTZ(currentTime:Date):String {
val timeZoneDate = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault())
return timeZoneString = timeZoneDate.format(currentTime)
}
Run Code Online (Sandbox Code Playgroud)
小智 5
Kotlin的最新解决方案:
private fun getDateFromMilliseconds(millis: Long): String {
val dateFormat = "MMMMM yyyy"
val formatter = SimpleDateFormat(dateFormat, Locale.getDefault())
val calendar = Calendar.getInstance()
calendar.timeInMillis = millis
return formatter.format(calendar.time)
}
Run Code Online (Sandbox Code Playgroud)
我们需要添加Locale作为SimpleDateFormat的参数或使用LocalDate。Locale.getDefault()是让JVM自动获取当前位置时区的好方法。
| 归档时间: |
|
| 查看次数: |
147438 次 |
| 最近记录: |