Hi the Conversion of String to Date is not possible here..
I search and use several methods but the error can not change..
here the date format is var q and convert that to formatedDate that is String
then the String convert into util date..
SearchDate is from method parameter and the value of SearchDate is "Thu Aug 29 00:00:00 IST 2013"
var q: Date = SearchDate
var dateStr = q.toString()
var formatter: DateFormat = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy")
var dat = formatter.parse(dateStr)
var cal: Calendar = Calendar.getInstance()
cal.setTime(dat)
var formatedDate = cal.get(Calendar.DATE) + "-" + (cal.get(Calendar.MONTH) + 1) + "-" + cal.get(Calendar.YEAR)
println("formatedDate : " + formatedDate)
val date = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(dateStr)// Error Occured Here..
the error is
Exception in thread "main" java.text.ParseException: Unparseable date: "Thu Aug 29 00:00:00 IST 2013"
at java.text.DateFormat.parse(Unknown Source)
please share your answers ..
You're trying to parse "E MMM dd HH:mm:ss Z yyyy" formatted date string as "yyyy-MM-dd HH:mm" formatted one, in this line:
val date = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(dateStr)
No wonder it fails.
Maybe you took the wrong date format for second SimpleDateFormat instance by mistake? Or maybe you're passing wrong parameter to parse() ?
edit
It looks like you actually wanted to call:
val date = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(dat)
Related
var date = 1624275605667;
final DateTime formatted = DateTime(date);
final DateFormat fr = DateFormat('EEE MMM d yyyy HH:mm:ss');
final String dd = fr.format(formatted);
I try like this but getting some type of errors.
I want to convert 1624275605667 into this format Mon Jun 21 2021 17:10:05 GMT+05:30
For this which format I use here
DateFormat('EEE MMM d yyyy HH:mm:ss zzz')
Please try this one
var date = 1624275605667;
final DateTime formatted = DateTime.fromMillisecondsSinceEpoch(date);
final DateFormat fr = DateFormat('EEE MMM dd yyyy HH:mm:ss');
final String dd = fr.format(formatted);
print(dd);
You are using z pattern and it's not implemented yet. Issue is still open since 2015 https://github.com/dart-lang/intl/issues/19
And in intl package already mentioned that this characters are reserved and currently are unimplemented.
For workaround you can use
formatted.timeZoneOffset.toString(); /// 5:30:00.000000
Which is same as GMT+05:30
I live by end of month dates such as "2019-02-28 23:59:59"
print when I print out that date it tells me "Mar-2019". No it is still "Feb-2019"
print(dt1.toString(dateFormat: "MMM-YYYY")
I do use SwiftDate. I also get this issue with the DateFormatter().
so instead of clean code I end up having to subtracting a day to get the correct month-year to display.
Why?
You need to set Timezone & date format properly as below,
let string = "2019-02-28 23:59:59"
let df = DateFormatter()
df.timeZone = .current
df.dateFormat = "yyyy-MM-dd HH:mm:ss"
let date = df.date(from: string)
print(date?.description(with: .current)) //Thursday, February 28, 2019 at 11:59:59 PM Gulf Standard Time
df.dateFormat = "MMM-yyyy"
print(df.string(from: date!)) // Feb-2019
I have this code:
var d = "22/12/1968 01:10:40"
var form : NSDateFormatter = NSDateFormatter()
form.dateFormat = "dd/MM/yyyy HH:mm:ss"
form.timeZone = NSTimeZone.localTimeZone() //Italy
print(form.dateFromString(d)!)
When I execute it I get this:
1968-12-22 00:10:40 +0000
first thing Why do I get "+0000" at end? and why the data format isn't respected?
Second , the time is wrong.
It's like the time is setted on London's time , but instead I setted it on my local time zone ( italy).
I tested it on iPhone Simulator
Why the date is wrong?
var d = "22/12/1968 01:10:40"
var form : NSDateFormatter = NSDateFormatter()
form.dateFormat = "dd/MM/yyyy HH:mm:ss"
//until here, there is no "problem"
form.timeZone = NSTimeZone.localTimeZone() //Italy //it means, the datetime is 22/12/1968 01:10:40 in Italy (GMT +1)
//Remember that NSDate is absolute time, or it holds GMT+0 time.
//As now, in Italy, it's 22/12/1968 01:10:40, then in GMT+0 time, it's 1h later. So, testDate holds 22/12/1968 00:10:40
NSDate testDate = form.dateFromString(d)!
Why it does not print the same format?
Because you did not use your NSDateFormatter object to get back date string but you used print() to print out directly the description of your object NSDate.
import org.joda.time._
import org.joda.time.format._
val pattern = "MMM d HH:mm:ss Z yyyy"
val input = "Apr 10 18:31:45 +0000 2015"
DateTime.parse(input, DateTimeFormat.forPattern(pattern))
anyone can tell me why it doesnt work ?
I get :
DateTime.parse(input, DateTimeFormat.forPattern(pattern))
java.lang.IllegalArgumentException: Invalid format: "Apr 10 18:31:45 +0000 2015"
at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:899)
at org.joda.time.DateTime.parse(DateTime.java:160)
... 43 elided
val pattern = "MMM d HH:mm:ss Z yyyy"
val input = "Apr 10 18:31:45 +0000 2015"
val format = DateTimeFormat.forPattern(pattern).withLocale(Locale.ENGLISH)
format.parseDateTime(input)
I found out nvm ;)
I want to convert a date to this format "dd Mon yyyy".
I have this code which works:
$date = [DateTime]::Parse("21/11/2014")
$dateFormatted = $date.GetDateTimeFormats()[12]
#$dateFormatted displays 21 November 2014
Is there a way to convert it using something like this?:
$dateFormatted = $date.ToString("dd Mon yyyy")
At the moment this returns "21 11on 2014"
I worked it out:
$dateFormatted = $date.ToString("dd MMMM yyyy")