如何在Java中获取日期时间的整数值?

huangapple go评论29阅读模式
英文:

How to get integer value of datetime in java?

问题

我正在进行一个Java项目,并需要在Java中获取DateTime的数值。例如:日期时间是2020-07-22T17:40:56.235+05:30,我想将其转换为20200722174056235。我正在使用DateTime的方法,如getDate()getYear()来创建这种类型的值。

是否有任何方法可以将日期时间转换为这种数值?

DateTime calendar = new DateTime();

int year       = calendar.getYear();
int month      = calendar.getMonthOfYear();
int dayOfMonth = calendar.getDayOfMonth();            
int hour       = calendar.getHourOfDay(); // 12小时制
int minute     = calendar.getMinuteOfHour();
int second     = calendar.getSecondOfMinute();
int millisecond= calendar.getMillisOfSecond();

String dt = String.valueOf(year) +
            String.valueOf(month) +
            String.valueOf(dayOfMonth) +
            String.valueOf(hour) +
            String.valueOf(minute) +
            String.valueOf(second) +
            String.valueOf(millisecond);
return Long.valueOf(dt);

我需要仅使用joda的DateTime

英文:

I'm working in a Java project and I need to get a numeric "value" of a DateTime in Java. For example: the datetime is 2020-07-22T17:40:56.235+05:30 and I want to convert it into 20200722174056235. I am using DateTime methods like getDate(), getYear() to make this kind of value.

Is there any way or any method to covert a datetime into such a numeric value?

DateTime calendar = new DateTime();

        int year       = calendar.getYear();
        int month      = calendar.getMonthOfYear();
        int dayOfMonth = calendar.getDayOfMonth();            
        int hour       = calendar.getHourOfDay();// 12 hour clock
        int minute     = calendar.getMinuteOfHour();
        int second     = calendar.getSecondOfMinute();
        int millisecond= calendar.getMillisOfSecond();
       
        String dt = String.valueOf((year)+
                String.valueOf(month)+
                String.valueOf(dayOfMonth)+
                String.valueOf(hourOfDay)+
                String.valueOf(minute)+
                String.valueOf(second)+
                String.valueOf(millisecond));
        return Long.valueOf(dt);

I need to use joda DateTime only.

答案1

得分: 3

使用格式化程序。

DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyyMMddHHmmssSSS");

DateTime calendar = new DateTime();
String formatted = calendar.toString(formatter);
Long numericValue = Long.parseLong(formatted);

System.out.println(numericValue);

在我的时区中运行代码时的输出:

> 20200722210458862

**备选方法:**只有在这是一个我预计会频繁调用且效率可能成为问题的库方法时,我可能会考虑不要格式化和解析字符串。以下代码可以得到相同的结果。

long numericValue = calendar.getYear();
numericValue = numericValue * 100 + calendar.getMonthOfYear();
numericValue = numericValue * 100 + calendar.getDayOfMonth();
numericValue = numericValue * 100 + calendar.getHourOfDay();
numericValue = numericValue * 100 + calendar.getMinuteOfHour();
numericValue = numericValue * 100 + calendar.getSecondOfMinute();
numericValue = numericValue * 1000 + calendar.getMillisOfSecond();

你的代码是否正常工作?

你的代码可能会将一位数值格式化为字符串中的一个字符,因此你的字符串通常会太短,缺少一些零。例如:

正确的:        20200722210458862 (2020年 07月 22日 21时 04分 58秒 862毫秒)
从你的代码中得到的: 202072221458862 (2020年  7月 22日 21时  4分 58秒 862毫秒)
英文:

Use a formatter.

	DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyyMMddHHmmssSSS");
	
	DateTime calendar = new DateTime();
	String formatted = calendar.toString(formatter);
	Long numericValue = Long.parseLong(formatted);
	
	System.out.println(numericValue);

Output when I ran the code in my time zone just now:

> 20200722210458862

Alternate way: Only if this is for a library method that I expect to be called often and where efficiency may be a concern, I might consider not formatting and parsing a string. The following gives the same result.

	long numericValue = calendar.getYear();
	numericValue = numericValue * 100 + calendar.getMonthOfYear();
	numericValue = numericValue * 100 + calendar.getDayOfMonth();
	numericValue = numericValue * 100 + calendar.getHourOfDay();
	numericValue = numericValue * 100 + calendar.getMinuteOfHour();
	numericValue = numericValue * 100 + calendar.getSecondOfMinute();
	numericValue = numericValue * 1000 + calendar.getMillisOfSecond();

Did your code work?

Your code probably formatted one-digit values into just one character in the string, so your string would typically be too short and miss some zeroes. For example:

Correct:        20200722210458862 (2020 07 22 21 04 58 862)
From your code:   202072221458862 (2020  7 22 21  4 58 862)

答案2

得分: -1

以下是代码片段的翻译:

public class ValidateString {
    public static void main(String[] args) {
        String pattern = "yyyyMMddHHmmss"; // 根据您的需求更改模式
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        String date = simpleDateFormat.format(new Date());
        System.out.println(date);
    }
}
英文:

Here is the code snippet

public class ValidateString {
    public static void main(String[] args) {
        String pattern = "yyyyMMddHHmmss"; // Change the pattern occording to your need
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        String date = simpleDateFormat.format(new Date());
        System.out.println(date);
    }
}

huangapple
  • 本文由 发表于 2020年7月22日 20:34:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63034315.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定