String to date conversion in Java exception - date

I know my question is deprecated but i can't really solve my problem.
Well i'm trying to convert a String to Date, but i'm getting unappeasable date exception. Here is my code:
String issued = "Thu, 31 Mar 2015 08:21:47 GMT";
SimpleDateFormat formatter2 = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
try {
Date date = formatter2.parse(issued);
Log.d("issued date", ""+date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Can any body tell me what is my problem !!
Thank you.

I've solved the problem, well t's a partial solution!.
String issued = "Thu, 02 Apr 2015 12:10:02";
SimpleDateFormat formatter2 = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss", Locale.US);
try {
Date date = formatter2.parse(issued);
Log.d("issued date", ""+date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
1) I added , Locale.US
2) I removed time zone (GMT)
Good luck.

Related

Change this "2022-07-26T12:10:07.000+0000" format to DateTime with 12 hrs format? Flutter

How i can extract the Time alone in this format "2022-07-26T12:10:07.000+0000" and show in 12hrs.
The output i am supposed to get is "5:40 PM", but i am getting 12.10 AM.
How to get exact time in the above mentioned format?
This is the method i followed to parse the DateTime.
String getTimeStringNew(String date) {
DateFormat dateFormat = DateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+SSSS");
var dateValue = dateFormat.parse(date);
DateFormat dateFormat1 = DateFormat("hh:mm a");
var value = dateFormat1.format(dateValue);
return value;}
In this method i am getting "12:10 AM".
But correct time is "5.40 PM".
What is the mistake i am doing here. Please correct.
2022-07-26T12:10:07.000+0000
Here 2022 is year
07 is month
26 is day
12 is hour
10 is minutes
07 is seconds.
So you will get 12.10 only
Maybe you are recieving the time in UTC.
You can use .toLocal to convert it to local time
var dateLocal = dateUtc.toLocal();
Edit
String getTimeStringNew(String date) {
DateTime _localDate = DateTime.parse(date).toLocal();
DateFormat dateFormat = DateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+SSSS");
var dateValue = dateFormat.format(_localDate);
DateFormat dateFormat1 = DateFormat("hh:mm a");
var value = dateFormat1.format(_localDate);
return value;}

Insert commas into string flutter

Say I have a string value as: June 22 2022 and I want to add comma with code such that it prints out this value: June 22, 2022. How do I achieve that?
In this particular case, you could use DateTime's DateFormatter:
import 'package:intl/intl.dart';
void main() {
DateFormat inputFormatter = DateFormat('MMMM dd yyyy');
DateFormat outputFormatter = DateFormat('MMMM dd, yyyy');
DateTime date = inputFormatter.parse('February 13 2042');
String formatted = outputFormatter.format(date);
print(formatted);
}
Console log
February 13, 2042

Changing Date and time Time formate from Json Response in dart?

Hello friend I am new in dart fetching data from Api now I want format this date time. In this way => Mar 20, 2021 11:36 am.. While json response coming this way =>":"2021-03-20 11:36:00
Here is my json full response file please check answer the question?
"message":"Data fetched successfully",
"data":[
{
"id":1,
"public_id":"ed0042ca-5a14-476b-9b65-66461efa81ae",
"title":"LIVE CONCERT",
"image":"https:\/\/livinghopemobile.com\/public\/storage\/event\/200321_113705_image.png",
"datetime":"2021-03-20 11:36:00", <=
"description":"LIVE CONCERT",
"created_at":"2021-03-20T06:37:05.000000Z",
"updated_at":"2021-03-29T07:18:50.000000Z"
}
]
}
String date = DateFormat("MMM dd, yyyy hh:mm a").format(DateTime.parse("2021-03-20 11:36:00"));
print(date); // =>Mar 20, 2021 11:36 AM
try out this
var date = "2021-03-24T08:57:47.812"
var dateTime = DateTime.parse("$date");
var stdTime = DateFormat('MMM d, yy hh:mm a').format(dateTime).toString();
Try this
var time = DateTime.parse(data[0]['datetime']);
And then you can look up here on the dates format list Here
var myTime = DateFormat('MMM d, yy hh:mm a').format(time);

Dart parse date with +0000

I am using the Twitter api for getting tweets in a Flutte App.
The api returns a formatted date like this:
Wed Jun 12 00:08:35 +0000 2019
DateTime formatTwitterDate() {
final format = DateFormat('EEE MMM dd hh:mm:ss +0000 yyyy'); //todo failed to resolve +0000
return format.parse(this);
}
This is the only formatter I got working. How can I support +0000?
Used this javascript implementation and transformed it to a DartExtension
https://stackoverflow.com/a/2611438/5115254
extension StringToDateTime on String {
DateTime formatTwitterDate() {
final newDateString = '${replaceAll('+0000 ', '')} UTC';
final format = DateFormat('EEE MMM dd hh:mm:ss yyyy Z');
return format.parse(newDateString);
}
}
Porting this answer to dart
var s = "Fri Apr 09 12:53:54 +0000 2010";
print(s);
final newString =
s.replaceAllMapped(RegExp(r'\w+ (\w+) (\d+) ([\d:]+) \+0000 (\d+)'), (m) {
return "${m[1]} ${m[2]} ${m[4]} ${m[3]} UTC";
});
print(newString);

How to parse date string in PKT time zone in Swift?

I'm having trouble parsing a date string in the PKT time zone.
Here is some code to paste in a playground:
import Foundation
let dateText = "Tue, 27 Jun 2017 04:00 AM PKT"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE, d MMM yyyy hh:mm a zzz"
if let checkDate = dateFormatter.date(from: dateText) {
let date = checkDate
print(date)
} else {
print("Could not parse")
}
If I change the time zone to "CDT" it works. However, I am trying to parse a date in Pakistan Standard Time (PKT) as that is the format I am parsing.
I found this handy tool and it says the format is invalid.
Using this table as a reference in formatting.
Documentation: https://developer.apple.com/documentation/foundation/nsdateformatter?language=objc
I see a references to similar questions How to get Current EST time IOS Swift