I am trying to get the System date and time and use that in the setFile() method to prevent overwriting my output files. Any idea how I can do that? I went down the path of Calendar.YEAR, etc. but that will give me model date and time not System. Any suggestions on how to proceed.
You can easily create a String of your current system time using:
Date currentDate = new Date(System.currentTimeMillis());
String dateAsString = currentDate.toString();
Use that in your setFile() method for the filename and you will never overwrite any outputs (unless you ran them in the same exact second :-) ).
After further investigation I was also able to implement using a SimpleDateFormat as follows
Date date = Calendar.getInstance().getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh.mm.ss");
String strDate = "myFile"+ dateFormat.format(date)+".txt"; myFile.setFile(strDate, myFile.WRITE);
Related
DateFormat dateFormat = DateFormat("yyyy-MM-dd"); This line of code will not let me change the DateFormat to ("MM-dd-yyyy") without showing throwing an error in the app, or not displaying correctly.
I just need it to show MONTH-DAY-YEAR, NOT YEAR-MONTH-DAY
Flutter refuses to let me change it. Nobody seems to have an answer for how to change it. I've been looking for two weeks on how to change it with no luck.
Also asked here: Flutter DatePicker change format to MM-DD-YYYY, but none of the answers worked either.
Here's the error being thrown in the app:
EDIT: More Code Context.
The original line was this:
DateFormat dateFormat = DateFormat("yyyy-MM-dd");
DateTime dateTime = dateFormat.parse(ticketModel.date);
dateTime was then getting shown via Text in a container this way:
child: Text(dateTime.toString().split(' ').first.toString(),
style: Theme.of(context).textTheme.bodyMedium!.copyWith(
letterSpacing: 2.5,
color: Colors.white,
)),
This is NOT my code, and I am not a Flutter dev.
Something is happening in these lines that won't allow MM-dd-yyyy to work properly.
The error is a photo, because it is getting displayed in the app container, not in any sort of terminal. I didn't want any mistakes if I tried copying it to text. I know its frowned upon, this isn't my first rodeo getting scolded on Stackoverflow, but I was desperate here.
My issue was solved for now by following that person's answer below and changing my code to this:
DateFormat dateFormat = DateFormat("yyyy-MM-dd");
DateTime fudgeThis = dateFormat.parse(ticketModel.date);
String dateTime = fudgeThis.showDateInOwnFormat();
Hopefully this edits help someone. Seriously.
let's say I have the DateTime variable
DateTime ourDateExample = DateTime.now();
you can create your own extension on DateTime to achieve what you want
extension ShowDataInOwnFormat on DateTime {
String showDateInOwnFormat() {
return '$month-$day-$year';
}}
put this outside of your classes or on a separate file, then call it whenever you want like this :
ourDateExample.showDateInOwnFormat();
it will return the date as the Month-day-year
Thank you for finally showing your code. However, the code you showed is not the code that generated the error. The point of asking for the exact code that you tried is not to be rude or sassy; it's to allow other people to reproduce your problem and to determine its exact cause, such as whether your problem was caused by a typo or by some other mistake. As evident from the error, what you actually tried was MMMM instead of MM, which would be a typo.
Based on your code and on the error message, I also suspect that you accidentally reused the same DateFormat object for parsing and printing. That is, you probably tried something like:
DateFormat dateFormat = DateFormat("MMMM-dd-yyyy");
DateTime fudgeThis = dateFormat.parse(ticketModel.date);
String dateTime = dateFormat.format(fudgeThis); // WRONG
That would not work if you're trying to convert between formats. You instead should have used separate DateFormat objects:
DateTime fudgeThis = DateFormat("yyyy-MM-dd").parse(ticketModel.date);
String dateTime = DateFormat("MM-dd-yyyy").format(fudgeThis);
Unlike naively creating your own string from the month, day, and year components directly, the above will format the date with the specified minimum number of digits.
Additionally, your error message indicates that you were attempting to parse an ISO-8601 date string, so you probably don't need to use DateFormat for parsing anyway:
DateTime fudgeThis = DateTime.parse(ticketModel.date);
String dateTime = DateFormat("MM-dd-yyyy").format(fudgeThis);
So I'm trying to get the current date and time in this format yyyy-MM-dd HH:mm:ss in a Date object, not a string.
How can I do this?
Keep in mind I can't use DateTimeFormatter.
Thanks.
Oh, but you can use DateTimeFormatter, as it's made exactly for this purpose.
var df:DateTimeFormatter = new DateTimeFormatter("");
df.setDateTimePattern("yyyy-MM-dd HH:mm:ss")
trace(df.format(new Date())); // Outputs: 2017-07-27 15:56:55
Is there a specific reason why this solution doesn't fit your needs?
I'm currently working with embarcadero c++, this is the first time I'm working with it so it's completely new to me.
What I'm trying to achieve is to get the current date, make sure the date has the "dd/MM/yyyy" format. When I'm sure this is the case I want to add a month to the current date.
So let's say the current date is 08/18/2016 this has to be changed to 18/08/2016 and then the end result should be 18/09/2016.
I've found that there is a method for this in embarcardero however I'm not sure how to use this.
currently I've only been able to get the current date like this.
TDateTime currentDate = Date();
I hope someone will be able to help me out here.
I figured it out.
After I've searched some more I found the way to use the IncMonth method on this page.
The example given my problem is as follows:
void __fastcall TForm1::edtMonthsExit(TObject *Sender)
{
TDateTime StartDate = edtStartDate->Text;
int Months = edtMonths->Text.ToInt();
TDateTime NextPeriod = IncMonth(StartDate, Months);
edtNextPeriod->Text = NextPeriod;
}
After looking at I changed my code accordingly to this
TDateTime CurrentDate = Date();
TDateTime EndDate = IncMonth(CurrentDate, 1);
A date object doesn't have a format like "dd/MM/yyyy". A date object is internally simply represented as a number (or possibly some other form of representation that really isn't your problem or responsibility).
So you don't have to check if it's in this format because no date objects will ever be in this format, they simply don't have a format.
You will have to do additions/subtractions on the Date object that the language or library gives you, THEN (optionally) you can format it to a human-readable string so it looks like 18/08/2016 or 18th of August 2016 or whatever other readable format that you choose.
It might be that the TRANSFER of a date between 2 systems is in a similar format, but then formatting the date like that is entirely up to you.
As for how to do that, the link you posted seems like a possible way (or alternatively http://docwiki.embarcadero.com/Libraries/Berlin/en/System.SysUtils.IncMonth), I'm afraid I can't give you an example as I'm not familiar with the tool/language involved, I'm just speaking generically about Date manipulations and they should ALWAYS be on the raw object.
I am trying to compare and check the date if it is today's date or not in a spesific program. I tried to use assertion method but when I use it the time will remain same if you try it next day. The main problem that I need to know when open a page from program It should be today's date and should be passed. if you know already anything about it please let me know also :)
Thanks yo!
Use System.DateTime.Now.ToString("yyyy-MM-dd") as one argument of the assertion. You may need to use a different format rather in the ...ToString() method. The exact format depends on how the date is shown on the screen.
This could be done using "StringAssert" to verify that your programs date string contains today's date string, while ignoring the time:
var programDateString = "7/25/2016 12:00:00"; //this is an example of your date retrieved from the application with time included
var todaysDate = System.DateTime.Today.ToShortDateString(); //short date string
StringAssert.Contains(programDateString, todaysDate);
I want to get the current time of the machine in GMT and presented it on custom format like this one using GWT:
yyyyMMddHHmmss
How I can achieve that?
I've tried this, but I didn't find how can I present this local time in GMT:
Date date = new Date();
DateTimeFormat dtf = DateTimeFormat.getFormat("yyyyMMddHHmmss");
System.err.println(dtf.format(date).toString());
Also note that the Date.getTimezoneOffset() is deprecated, which I could use to subtract it from the current date and format it afterwards, but it doesn't sound like a good plan.
Date date = new Date();
DateTimeFormat dtf = DateTimeFormat.getFormat("yyyyMMddHHmmss");
Window.alert(dtf.format(date, TimeZone.createTimeZone(0)));
You need to import com.google.gwt.i18n.client.TimeZone and not java.util.TimeZone.