我认为2011-10-23 12:00:00
将保持相同UTC
,而且Converted date
会2011-10-23 17:00:00
.
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dt = formatter.parse("2011-10-23 12:00:00");
LocalDateTime ldt = new DateTime(dt).withZone(DateTimeZone.UTC).toLocalDateTime();
LOGGER.warn("Original date: " + ldt.toDateTime().toDate().toString());
DateTime cvtldt = ldt.toDateTime(DateTimeZone.forID("-05:00"));
LOGGER.warn("Converted date: " + cvtldt.toLocalDateTime().toDateTime().toDate().toString());
Run Code Online (Sandbox Code Playgroud)
我不明白为什么输出减去一个小时?
Original date: Sun Oct 23 11:00:00 BST 2011
Converted date: Sun Oct 23 11:00:00 BST 2011
Run Code Online (Sandbox Code Playgroud) 如果可能的话,我希望joda或非joda解决方案适用于下面的场景
让我们说如果我的一周从02/05/2012开始,并且给定的当前日期是02/22/2011.我需要计算给定当前日期的周开始日期和结束日期.因此,我的解决方案应该在02/19开始一周,在02/25结束一周.为简单起见,我已将我的周开始时间设置为02/05/2011,但它可能是任何一天,我的周总有7天.
我现有的代码如下,但似乎没有按预期工作.
public Interval getWeekInterval(Date calendarStartDate, Date date)
{
Calendar sDate = Calendar.getInstance();
sDate.setTime(getMidnightDate(calendarStartDate));
Calendar eDate = Calendar.getInstance();
eDate.setTime(date);
Calendar weekStartDate = (Calendar) sDate.clone();
logger.debug("Date:" + sDate.getTime());
while (sDate.before(eDate)) {
weekStartDate = sDate;
sDate.add(Calendar.DAY_OF_WEEK_IN_MONTH, 1);
}
return new Interval(weekStartDate.getTime(), sDate.getTime());
}
Run Code Online (Sandbox Code Playgroud) 我在Coldfusion中使用Java对象,所以我的代码有点偏.
我有一个看起来像这样的函数:
function getJODAOffset(sTimezone){
local.oDateTimeZone = createObject('java','org.joda.time.DateTimeZone');
local.oInstant = createObject('Java','org.joda.time.Instant');
local.oFormatter = createObject("Java",'org.joda.time.format.DateTimeFormat');
local.oFormatter = local.oFormatter.forPattern('ZZ');
local.tTime = local.oDateTimeZone.forID(arguments.sTimezone).getStandardOffset(local.oInstant); //sTimezone = 'Europe/London';
return local.oFormatter.withZone(local.oDateTimeZone.forID(arguments.sTimezone)).print(local.tTime);
}
Run Code Online (Sandbox Code Playgroud)
当我期待" +00:00 "时,这给了我" +01:00 " 的输出,我不知道为什么.
我想在日期上加1个月,但问题是当我有2月28日的日期然后我再添加一个月,我的结果是3月28日.它应该在3月31日结束.当我开始4月的日期时30,我应该在5月31日而不是5月30日结束.我尝试使用Jodatime的plusMonths()和java.util.date的add()但仍然是同样的问题.
我有这个字符串
23/Gennaio/2014
我需要另一个字符串
23/01/2014
我尝试使用joda.time:
DateTimeFormatter format = DateTimeFormat.forPattern("dd/MMM/yyyy").withLocale(Locale.ITALY);
DateTime instance = format.parseDateTime("23/Gennaio/2014");
String month_number = String.valueOf(instance.getMonthOfYear());
Run Code Online (Sandbox Code Playgroud)
但是我得到了这个例外:
01-06 13:31:55.341:E/AndroidRuntime(1116):java.lang.IllegalArgumentException:格式无效:"23/Gennaio/2014"
我错过了什么?
我正在尝试使用12:00的小时初始化Joda-Time DateTime对象,这是我如何做到这一点:
public static final long MINUTE = 60 * 1000;
public static final long HOUR = 60 * MINUTE;
DateTime defaultDate = new DateTime(HOUR * 12);
System.out.print("the hour is: " + defaultDate.getHourOfDay()) // getting 14
Run Code Online (Sandbox Code Playgroud)
为什么我得到14而不是12?也许妈妈没教我怎么读时钟吧?!
我有这个简单的代码:
DateTimeFormatter format = DateTimeFormat.forPattern("YYYY MM DD");
DateTime dateTime;
String buffer = "2013 01 01";
dateTime = format.parseDateTime(buffer);
System.out.println(dateTime.getMonthOfYear());
buffer = "2013 02 01";
dateTime = format.parseDateTime(buffer);
System.out.println(dateTime.getMonthOfYear());
Run Code Online (Sandbox Code Playgroud)
输出为:1 1
但我希望:1 2
...问题出在哪儿?我不需要一年中的一个月,但我确定dayOfWeek有问题,因为dateTime始终是1月而不是其他月份.
我之前从未使用过Joda-Time,但我有ArrayList,其中包含具有LocalDate和count的对象.所以我在ArrayList中计算每一天,每天只在ArrayList中使用一次.我需要计算一年中每个月的计数,这在列表中.
我的数据:例如:
dd.MM.yyyy
17.01.1996 (count 2)
18.01.1996 (count 3)
19.02.1996 (count 4)
19.03.1996 (count 1)
18.05.1997 (count 3)
Run Code Online (Sandbox Code Playgroud)
现在我想要outpur像这样:
MM.yyyy
01.1996 -> 2 (17.1.1996) + 3 (18.1.1996) = 5
02.1996 -> 4 (19.2.1996) = 4
03.1996 -> 1 (19.3.1996) = 1
05.1997 -> 3 (18.5.1997) = 3
Run Code Online (Sandbox Code Playgroud)
我只需要每个月计算,但我不知道最好的方法是什么.
数据类:
private class Info{
int count;
LocalDate day;
}
Run Code Online (Sandbox Code Playgroud)
结果我会在一些包含月份和年份日期+计数的类中输入.
如何使用Joda-Time将天数转换为月数和天数.例如,当我有33天时,它应该显示1个月和2天.
public static void main(String[]args)
{
Calendar calendar = new GregorianCalendar();
int years = calendar.get(Calendar.YEAR);
int month = (calendar.get(Calendar.MONTH))+1;
int day = calendar.get(Calendar.DATE);
DateTime startDate = new DateTime(years, month, day, 0, 0, 0, 0);
DateTime endDate = new DateTime(2014, 7, 3, 0, 0, 0, 0);
Days d = Days.daysBetween(startDate, endDate);
int days = d.getDays();
int t = 1000 * 60 * 60 *24;
int days = days/t;
System.out.println(days);
}
Run Code Online (Sandbox Code Playgroud) 我想找到两个日期/时间之间的日,小时,分钟和秒.我以为我可以使用句号,我尝试了下面的代码,但我得到了一个非荒谬的答案.
DateTime dt1 = new DateTime("2004-12-13T21:39:45.618-06:00");
DateTime dt2 = new DateTime("2004-12-13T21:39:45.618-08:00");
Period p = new Period(dt1, dt2);
System.out.println("Test: " + p);
Run Code Online (Sandbox Code Playgroud)
从这里我得到输出:
I/System.out: Test: PT2H
Run Code Online (Sandbox Code Playgroud)
不确定这是什么意思.
谢谢你的帮助