As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
I have a String 2012-10-23 which I need to convert into a Date object.
Can I pass this string directly to the below function
Date date = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse(string);
Date date = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).parse(string);
for 2012-10-23 your format should be "yyyy-MM-dd"
String string = "2012-10-23";
Date date = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).parse(string);
Letter Date or Time Component Presentation Examples
G Era designator Text AD
y Year Year 1996; 96
Y Week year Year 2009; 09
M Month in year Month July; Jul; 07
w Week in year Number 27
W Week in month Number 2
D Day in year Number 189
d Day in month Number 10
F Day of week in month Number 2
E Day name in week Text Tuesday; Tue
u Day number of week (1 = Monday, ..., 7 = Sunday) Number 1
a Am/pm marker Text PM
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
m Minute in hour Number 30
s Second in minute Number 55
S Millisecond Number 978
z Time zone General time zone Pacific Standard Time; PST; GMT-08:00
Z Time zone RFC 822 time zone -0800
X Time zone ISO 8601 time zone -08; -0800; -08:00
No you can't, this is how
String string = "2012-10-23";
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(string);
Related
The LocalDateTime API gives the possibility to add the TimeZone Name by using the key "z" in the formatter. I get an exception adding this key and don't understand why. I'm looking for something like this example '11:59:22 PM GMT' and not '**... UMT+2**'.
My Code:
public class TimeZone
{
public static void main(String[] args)
{
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm:ss a z");
System.out.println(now.format(formatter));
}
}
The Exception:
Exception in thread "main" java.time.DateTimeException: Unable to extract value: class java.time.LocalDateTime
at java.time.format.DateTimePrintContext.getValue(Unknown Source)
at java.time.format.DateTimeFormatterBuilder$ZoneTextPrinterParser.format(Unknown Source)
at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(Unknown Source)
at java.time.format.DateTimeFormatter.formatTo(Unknown Source)
at java.time.format.DateTimeFormatter.format(Unknown Source)
at java.time.LocalDateTime.format(Unknown Source)
at tz.TimeZone.main(TimeZone.java:12)
Here is the DateTimeFormatter API part:
All letters 'A' to 'Z' and 'a' to 'z' are reserved as pattern letters. The following pattern letters are defined:
Symbol Meaning Presentation Examples
------ ------- ------------ -------
G era text AD; Anno Domini; A
u year year 2004; 04
y year-of-era year 2004; 04
D day-of-year number 189
M/L month-of-year number/text 7; 07; Jul; July; J
d day-of-month number 10
Q/q quarter-of-year number/text 3; 03; Q3; 3rd quarter
Y week-based-year year 1996; 96
w week-of-week-based-year number 27
W week-of-month number 4
E day-of-week text Tue; Tuesday; T
e/c localized day-of-week number/text 2; 02; Tue; Tuesday; T
F week-of-month number 3
a am-pm-of-day text PM
h clock-hour-of-am-pm (1-12) number 12
K hour-of-am-pm (0-11) number 0
k clock-hour-of-am-pm (1-24) number 0
H hour-of-day (0-23) number 0
m minute-of-hour number 30
s second-of-minute number 55
S fraction-of-second fraction 978
A milli-of-day number 1234
n nano-of-second number 987654321
N nano-of-day number 1234000000
V time-zone ID zone-id America/Los_Angeles; Z; -08:30
z time-zone name zone-name Pacific Standard Time; PST
O localized zone-offset offset-O GMT+8; GMT+08:00; UTC-08:00;
X zone-offset 'Z' for zero offset-X Z; -08; -0830; -08:30; -083015; -08:30:15;
x zone-offset offset-x +0000; -08; -0830; -08:30; -083015; -08:30:15;
Z zone-offset offset-Z +0000; -0800; -08:00;
Can anyone see what the problem is?
LocalDateTime has two fields of type LocalDate and LocalTime.
LocalDate has fields day, month, and year.
LocalTime has fields hour, minute, second, and nano.
Nowhere in that is a time zone given. Which is by nature, since the javadoc of LocalDateTime says:
A date-time without a time-zone
So, if the "local" date/time value is already representing a time in UTC, and you want it formatted saying so, you have multiple options:
Change the LocalDateTime to a ZonedDateTime by calling atZone():
System.out.println(time.atZone(ZoneOffset.UTC)
.format(DateTimeFormatter.ofPattern("hh:mm:ss a z")));
Specify an override time zone in the formatter by calling withZone():
System.out.println(time.format(DateTimeFormatter.ofPattern("hh:mm:ss a z")
.withZone(ZoneOffset.UTC)));
Format with a literal Z character:
System.out.println(time.format(DateTimeFormatter.ofPattern("hh:mm:ss a 'Z'")));
All three of the above outputs:
11:59:22 PM Z
Now, if the "local" date/time is really in a different time zone, you can use either of the first two, and just specify the actual zone.
E.g. if time zone is -04:00, use ZoneOffset.ofHours(-4), and you'll get:
11:59:22 PM -04:00
Or if you are in New York, use ZoneId.of("America/New_York"), and you'll get:
11:59:22 PM EDT
If the "local" date/time is for New York, but you want formatted text to be UTC, use both at the same time, i.e.
System.out.println(time.atZone(ZoneId.of("America/New_York"))
.format(DateTimeFormatter.ofPattern("hh:mm:ss a z")
.withZone(ZoneOffset.UTC)));
With that, you get the time converted to:
03:59:22 AM Z
The LocalDateTime class does not support time zones; you can use ZonedDateTime instead.
ZonedDateTime now = ZonedDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm:ss a z");
System.out.println(now.format(formatter));
This no longer throws an exception and prints 06:08:20 PM EDT for me, but the timezone will differ depending on your location.
I have seen this, but is it possible to use only month and days, not weeks?
For example, instead of "1 month 2 weeks 2 days", I want "1 month 16 days".
You can use a solution similar to the answer you linked, but you'll also need to use a org.joda.time.PeriodType to normalized the period:
// 1 month, 2 weeks and 2 days
Period p = new Period(0, 1, 2, 2, 0, 0, 0, 0);
PeriodFormatter fmt = new PeriodFormatterBuilder()
// months
.appendMonths().appendSuffix(" month", " months").appendSeparator(" and ")
// days
.appendDays().appendSuffix(" day", " days")
.toFormatter();
System.out.println(fmt.print(p.normalizedStandard(PeriodType.yearMonthDayTime())));
This will print:
1 month and 16 days
This question already has answers here:
Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...
(32 answers)
Closed 8 years ago.
My Question is Little bit tricy .
i have to display date difference. (i find out date difference in terms of 2 year 7 month 3 days 5 hrs 30 min.)
Now how i display upto exact 2 higher values
please consider given case
case 1 : date difference is 0 year 2 month 21 days 7 hrs 30 min
output must be : 2 Month 21 days
case 2 : 0 year 0 month 0 days 7 hrs 20 min
output must be : 7 hours 21 days
If your date differences is in the exact form as in your question (separated with " ") and formatted as strings. This will do the trick.
<?php
function display_times($string){
$pieces = explode(" ",$string);
$num_disp = 0;
foreach($pieces as $i => $pice){
if(is_numeric($pice) && intval($pice) != 0){
echo $pice." ".$pieces[$i+1]." ";
$num_disp++;
if($num_disp >= 2) break;
}
}
}
$case1 = "0 year 2 month 21 days 7 hrs 30 min";
$case2 = "0 year 0 month 0 days 7 hrs 20 min";
display_times($case1);
echo PHP_EOL;
display_times($case2);
?>
You can modify this function. Replace 3rd parameter with $depth and modify array_slice line, like on demo.
Use examples :
echo time_diff_string('2013-05-01 00:22:35', 'now', 1), "\n";
echo time_diff_string('2013-05-01 00:22:35', 'now', 2), "\n";
echo time_diff_string('2013-05-01 00:22:35', 'now', 3), "\n";
echo time_diff_string('2013-05-01 00:22:35', 'now', 4), "\n";
Output :
6 months ago
6 months, 6 days ago
6 months, 6 days, 12 hours ago
6 months, 6 days, 12 hours, 6 minutes ago
Demo.
What is wrong? I assume that if I subtract 1ms from 1 Jan 1980 0:0:0 then I've got 1979. But I must subtract about 500+ ms for this. Please, give me a hint.
val cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"))
cal.set(1980, 0, 1, 0, 0, 0)
val date = new Date
date.setTime(cal.getTimeInMillis()) // <- 1980 Jan 01 0:0:0
date.setTime(cal.getTimeInMillis() - 1) // <- 1980 Jan 01 0:0:0 too !!!
Updated.
The solution is
val cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"))
cal.setTimeInMillis(0)
cal.set(1980, 0, 1, 0, 0, 0)
With Calendar.set(year, month, day, hourOfDay, minute, second) no milliseconds are set. Consequently the Calendar implementation sets the milliseconds to "unknown" which is actually treated as the midpoint within the given second.
Subtracting 500ms means you just step over the midpoint. Same should happen if you add 500ms, which should bring you just over the second. Actually subtracting 500ms works and you must add 620ms to see the next second.
End date was being computed to be earlier than the start date
Date startDate = new Date();
Date endDate = new Date(startDate.getTime() + (24 * 3600000 * 42));
System.out.println(startDate);
System.out.println(endDate);
output :
Tue Sep 17 01:46:31 EEST 2013
Mon Sep 09 08:43:43 EEST 2013
why the output is not correct ?
Your integer arithmetic has overflowed. The maximum possible value of an int is 2147483647 or Integer.MAX_VALUE (a little over 2 billion), but multiplying your integer literals would yield 3628800000 (about 3.6 billion). The result is a negative number (-666167296), and an earlier date.
Try casting one of your literals as a long to force long arithmetic (or use long literals):
( (long) 24 * 3600000 * 42)
or
(24L * 3600000 * 42)
This operation is well within the range of long values (max value 9223372036854775807, over 9 quintillion).
24 * 3600000 * 42 is 3,628,800,000 which does not fit into an int. Rollover occurs. Force the use of a long by casting one of the factors:
24L * 3600000 * 42
The number you're trying to add is 24 * 3600000 * 42 which is equal to 3,628,800,000. This is larger than 2,147,483,647 which is the maximum value that can be represented with the given data type. What you're experiencing is an overflow, meaning that after crossing the maximum value, the number loops back to its lowest value, which is in the negative. Therefore, you're adding a negative value to the date.