Force Talend to use UTC for Dates - talend

I have a set of dates stored as UTC in my database, when I import them in Salesforce using the tSalesforceOutput:
If I run the import from my machine, they get the wrong timezone
If I run the import from a server which is in UTC, they get the correct timezone.
Is Talend/Salesforce API using the local timezone? How can I prevent this?

Salesforce will allways convert datetime from user timezone to UTC before to store them.
To avoid any problem, the simpler is to fix the user timezone used for the Salesforce connection to GMT and to explicitly convert each datetime to this timezone before to call any tSalesforceOutputXxxx component.
Here is a routine you can use for this purpose:
package routines;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class dateConversion {
public static String convertToGmt(String strDate, String timezone) throws Exception
{
if (strDate == null || timezone == null)
return null;
// Convert strDate from any valid TimeZone such as Europe/Paris to GMT
// strDate is expected to be formatted as "yyyy-MM-ddTHH:mm:ssZ"
SimpleDateFormat indfm = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
indfm.setTimeZone(TimeZone.getTimeZone(timezone));
Date inDate = indfm.parse(strDate);
SimpleDateFormat outdfm = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
outdfm.setTimeZone(TimeZone.getTimeZone("GMT"));
String s = outdfm.format(inDate);
return s;
}
}
Hope this helps.
TRF

Related

How to convert to readable dates in Dart

I would like to know how a date such as "2022-07-17T01:46:12.632892+05:30" be converted to a Human Readable date in DD/MM/YYYY and hh:mm:ss format? I probably have not surfed through a lot of other questions and suggestions on the Internet but the ones I came across were not of any help. Also, what are such date formats(like the one in question) called?
It is rather straightforward using DateFormat from the package intl which comes with Flutter:
import 'package:intl/intl.dart';
void main() {
final dateTime = DateTime.parse('2022-07-17T01:46:12.632892+05:30').toUtc();
final dateFormat = DateFormat('dd/MM/yyyy HH:mm:ss');
print(dateFormat.format(dateTime)); // 16/07/2022 20:16:12
}
The time has here been converted to UTC to make the example the same for all readers. If not, the created DateTime would be localtime which uses the timezone on the device which the program are running.
If you want to print the time using the timezone offset of 5 hours and 30 minutes, you can do something like this:
import 'package:intl/intl.dart';
void main() {
final dateTime = DateTime.parse('2022-07-17T01:46:12.632892+05:30').toUtc();
final dateFormat = DateFormat('dd/MM/yyyy HH:mm:ss');
print(dateFormat.format(dateTime.add(const Duration(hours: 5, minutes: 30))));
// 17/07/2022 01:46:12
}

Flutter DateTime error on string to DateTime [duplicate]

Say I have a string
"1974-03-20 00:00:00.000"
It is created using DateTime.now(),
how do I convert the string back to a DateTime object?
DateTime has a parse method
var parsedDate = DateTime.parse('1974-03-20 00:00:00.000');
https://api.dartlang.org/stable/dart-core/DateTime/parse.html
There seem to be a lot of questions about parsing timestamp strings into DateTime. I will try to give a more general answer so that future questions can be directed here.
Your timestamp is in an ISO format. Examples: 1999-04-23, 1999-04-23 13:45:56Z, 19990423T134556.789. In this case, you can use DateTime.parse or DateTime.tryParse. (See the DateTime.parse documentation for the precise set of allowed inputs.)
Your timestamp is in a standard HTTP format. Examples: Fri, 23 Apr 1999 13:45:56 GMT, Friday, 23-Apr-99 13:45:56 GMT, Fri Apr 23 13:45:56 1999. In this case, you can use dart:io's HttpDate.parse function.
Your timestamp is in some local format. Examples: 23/4/1999, 4/23/99, April 23, 1999. You can use package:intl's DateFormat class and provide a pattern specifying how to parse the string:
import 'package:intl/intl.dart';
...
var dmyString = '23/4/1999';
var dateTime1 = DateFormat('d/M/y').parse(dmyString);
var mdyString = '04/23/99';
var dateTime2 = DateFormat('MM/dd/yy').parse(mdyString);
var mdyFullString = 'April 23, 1999';
var dateTime3 = DateFormat('MMMM d, y', 'en_US').parse(mdyFullString));
See the DateFormat documentation for more information about the pattern syntax.
DateFormat limitations:
DateFormat cannot parse dates that lack explicit field separators. For such cases, you can resort to using regular expressions (see below).
Prior to version 0.17.0 of package:intl, yy did not follow the -80/+20 rule that the documentation describes for inferring the century, so if you use a 2-digit year, you might need to adjust the century afterward.
As of writing, DateFormat does not support time zones. If you need to deal with time zones, you will need to handle them separately.
Last resort: If your timestamps are in a fixed, known, numeric format, you always can use regular expressions to parse them manually:
var dmyString = '23/4/1999';
var re = RegExp(
r'^'
r'(?<day>[0-9]{1,2})'
r'/'
r'(?<month>[0-9]{1,2})'
r'/'
r'(?<year>[0-9]{4,})'
r'$',
);
var match = re.firstMatch(dmyString);
if (match == null) {
throw FormatException('Unrecognized date format');
}
var dateTime4 = DateTime(
int.parse(match.namedGroup('year')!),
int.parse(match.namedGroup('month')!),
int.parse(match.namedGroup('day')!),
);
See https://stackoverflow.com/a/63402975/ for another example.
(I mention using regular expressions for completeness. There are many more points for failure with this approach, so I do not recommend it unless there's no other choice. DateFormat usually should be sufficient.)
import 'package:intl/intl.dart';
DateTime brazilianDate = new DateFormat("dd/MM/yyyy").parse("11/11/2011");
you can just use : DateTime.parse("your date string");
for any extra formating, you can use "Intl" package.
void main() {
var dateValid = "30/08/2020";
print(convertDateTimePtBR(dateValid));
}
DateTime convertDateTimePtBR(String validade)
{
DateTime parsedDate = DateTime.parse('0001-11-30 00:00:00.000');
List<String> validadeSplit = validade.split('/');
if(validadeSplit.length > 1)
{
String day = validadeSplit[0].toString();
String month = validadeSplit[1].toString();
String year = validadeSplit[2].toString();
parsedDate = DateTime.parse('$year-$month-$day 00:00:00.000');
}
return parsedDate;
}
a string can be parsed to DateTime object using Dart default function DateTime.parse("string");
final parsedDate = DateTime.parse("1974-03-20 00:00:00.000");
Example on Dart Pad
String dateFormatter(date) {
date = date.split('-');
DateFormat dateFormat = DateFormat("yMMMd");
String format = dateFormat.format(DateTime(int.parse(date[0]), int.parse(date[1]), int.parse(date[2])));
return format;
}
I solved this by creating, on the C# server side, this attribute:
using Newtonsoft.Json.Converters;
public class DartDateTimeConverter : IsoDateTimeConverter
{
public DartDateTimeConverter()
{
DateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFK";
}
}
and I use it like this:
[JsonConverter(converterType: typeof(DartDateTimeConverter))]
public DateTimeOffset CreatedOn { get; set; }
Internally, the precision is stored, but the Dart app consuming it gets an ISO8601 format with the right precision.
HTH

how to show timestamp from firestore in dd/mm/yy format in flutter

how to show datetime(timestamp format) form firebase firestore in (dd/mm/yy, hh:mm:ss) in flutter.
please see the images attachedfirebase firestore data
and my code is my code in vscode
You can simply call toDate() function to the dateTime or your firebase timestamp.
You can also convert them into desired format by using DateFormat class
Here is a small function which will return time like 12:37 AM :
import 'package:intl/intl.dart'; //add this import statement for using DateTime class
String getTime(var time) {
final DateFormat formatter = DateFormat('dd/MM/yyyy, hh:mm:ss aa'); //your date format here
var date = time.toDate();
return formatter.format(date);
}
This function will convert your timestamp object to provided format
eg.: July 23, 2021 at 9:22:29 PM UTC+5:30 -> 23/07/2021, 9:22:29 PM
You can refer this document for detailed date formatting.
You can first parse the date to get a DateTime object by using DateTime.parse(string_from_firebase).
Then use the DateFormat class from the intl package.
final DateTime dateToBeFormatted = DateTime.parse(string);
final df = DateFormat('dd/MM/yyyy');
final formatted = df.format(dateToBeFormatted);

Convert a jmeter variable of milliseconds to a formatted date

I cant seem to convert a date in milliseconds (1488520800000) extracted from JSON and put into a variable into a formatted date (2017-03-02). Here's my code:
import java.text.*;
import java.util.*;
SimpleDateFormat source = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
SimpleDateFormat target = new SimpleDateFormat("yyyy-MM-dd");
Date date = source.parse(vars.get("varReviewDatevalue"));
String newDate = target.format(date);
vars.put("varFormattedReviewdateValue",newDate);
Here's the error I get:
ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import java.text.*; import java.util.*; log.info("value for variable: 14885208 . . . '' : Typed variable declaration : Method Invocation source.parse
What's weird is that I got similar code to work fine for an extracted date like: March 2, 2017. I can't figure out why the date represented in mills is not converting to a date. Any ideas?
I was using the wrong jmeter element. This post helped me alot: JMeter: Converting extracted time stamp value to date format
I put this code into a JSR223 Sampler and everything worked
import java.text.*;
long timeStamp = Long.parseLong(vars.get("varReviewDatevalue"));
Date date = new Date(timeStamp);
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
TimeZone tzInAmerica = TimeZone.getTimeZone("America/Denver");
formatter.setTimeZone(tzInAmerica);
String dateFormatted = formatter.format(date);
vars.put("varFormattedReviewdateValue", dateFormatted);
log.info(dateFormatted);
log.info(vars.get("varFormattedReviewdateValue"));

Date changing from Groovy to String

I have to find the last date of the last month
I am using a Groovy Script
I can get the date part ok but now I have to turn it into a String in the form yyyyMMdd.
Code so far (and yes it works)
import java.util.GregorianCalendar;
import java.util.Calendar;
import java.util.Date;
Calendar aCalendar = Calendar.getInstance();
// add -1 month to current month
aCalendar.add(Calendar.MONTH, -1);
// set actual maximum date of previous month
aCalendar.set(Calendar.DATE,aCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
//read it
lastDateOfPreviousMonth = aCalendar.getTime();
This returns the date in timestamp form
20160229 105925.240
Now I need to extract 20160229 from the timestamp as a string
I've tried just about everything...
If this is running on Java 8, you can use the new Java Time classes:
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.time.temporal.TemporalAdjusters
String date = LocalDate.now()
.minusMonths(1)
.with(TemporalAdjusters.lastDayOfMonth())
.format(DateTimeFormatter.ofPattern("yyyyMMdd"))
If this is Java 7 or Java 6, you can use Calendar and a bit of Groovy:
String date = Calendar.instance.with {
add(MONTH, -1)
set(DAY_OF_MONTH, getActualMaximum(DAY_OF_MONTH))
time
}.format('yyyyMMdd')