multiple timestamps in one program execution [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Need help!
I am not able to get multiple timestamps in one execution,
basically i am running a test suite , and i am using ordernumbers as the current timestamp in hhmmss,
But when first time timestamp is taken, it has the same value throughout the execution, i want changing current values, during the execution.
code used :
static DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd-hhmmss");
static Date date = new Date();
public static String ordernum = dateFormat.format(date);

You're always reusing the same Date object. You need to create a new Date() every time you need a new timestamp.
Update
Took another look at your question: using a second-precision timestamp is probably not the best guarantee for uniqueness, especially in test execution.
Why not do:
String ordernum = java.util.UUID.randomUUID().toString();
Or:
String ordernum = "" + System.nanoTime();

Related

Java - GSON convert timestamp [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 days ago.
Improve this question
this is my input json string
{
"dateOfBirth" : "2023-02-16"
"recored_created_with_timestamp" : "2023-02-16 10:20:30"
}
Using GSON I just want to convert recored_created_with_timestamp with timestamp, and dateOfBirh field date format remains should be same.
setDateFormat("yyyy-MM-dd hh:mm:ss")
When I set above the first filed throwing exception...
class Person {
private Date dateOfBirth;
private Timestamp recored_created_with_timestamp
}

Fetch value from Map<Object, Object> for a particular key in java [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have a Map<Object, Object> in Java, and I want to fetch value for a particular key.
sampleMap = { id = customerName , year = 2020, month = March }
I want to do something like
String name = sampleMap.get("month").toString();
But this is not working for me. How do I fetch values for a particular key in this case.
Note: I don't want to run a loop and check for each key as the Map is quite big in this case.
According to the data given in the question and comments, below code will fix your issue.
String name = sampleMap.containsKey("month") ? (
(sampleMap.get("month") != null) ? sampleMap.get("month").toString() : "N/A")
: "N/A";
You will get java.lang.NullPointerException when the key is not available or the value is null. So you should perform key check and null check before acquire data using the key.

MilliSec To Date C++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a java program which makes a JNI call. From java, I am sending Date.getTime() (basically seconds * 1000), and I expect C++ to convert it to a perfect date.
NOT DUPLICATE: I KNOW MANY SOLUTIONS EXIST ON STACKOVERFLOW, BUT NONE OF THEM SEEM TO WORK WITH DATES SINCE 01-01-0001
I need the dates to get perfectly generated, for as early as 01-01-0001 00:00:00.
Any clue or code snippet in C++?
This assumes that getTime() works according to the java docs outputting time since 1970.
Try this:
How can I convert seconds since the epoch to hours/minutes/seconds in Java?
(Edit: even though the question is for Java the implementation given in the first answer is in C, which is (mostly) valid C++ code. It doesn't account for leap seconds so just subtract the number of leap seconds since 1970 before evaluating)

Convert JavaUtilDate to NSDate and vice versa? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I use j2objc to translate a java util Date to a objective-c JavaUtilDate class.
I imported #import "java/util/Date.h"to store a translated java Date variable.
var myDate: JavaUtilDate
How do I convert a JavaUtilDate into an NSDate?
Depending on where/how you get the Java Date, your best bet would be to get the milliseconds and instantiate the NSDate with it.
So call the getTime() method on the Java Date to get milliseconds since Epoch, then create your NSDate with the dateWithTimeIntervalSince1970: method. The NSDate method takes seconds (as a floating point), so divide by 1000.0 to keep the precision (thanks to Martin R for pointing this out in comments). :)
Java Util Date method
NSDate method
There seems to be some confusion on what exactly is being asked. To be as general as possible, time objects typically have a method to get the milliseconds since epoch and a constructor (or setter) to pass in the seconds since epoch. So all you have to do is get the seconds from one object and instantiate the other object with the seconds.

How to replace string with another string by index? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a string like this- 11/15/2013.
I want to replace 2013 with 2000 (last 2 digits with 0). how to do this?
You can always do replace date.replace("13", "00");
but if you are looking at something generic of a solution (In case if you are not aware of what the last 2 digits are, yet you want them to be '00') you can do something like this :
String date = "11/15/2013";
String newDate = date.substring(0,8)+"00";
Or you can use a StringBuilder:
StringBuilder date = new StringBuilder("11/15/2013");
date.setCharAt(8, '0');
date.setCharAt(9, '0');
or
StringBuilder date = new StringBuilder("11/15/2013");
date.replace(8, 10, "00");
code snippet
String date = "11/15/2013";
String replaced = date.replace("2013","2000");
If you really want to replace a sequence at a certain index of a String, use a StringBuilder:
String replaced = new StringBuilder(date)
.replace(8, 10, "00").toString();
String date = "11/15/2013";
System.out.println(date.substring(0, date.length()-2).concat("00"));
Is this what you are looking for?
Better code snippet
String replaced = date.replace("11/15/2013.","11/15/2000");

Categories

Resources