我想计算两个日期之间的时差(格式为"yyyyMMddHHmmss").基本思想是首先将字符串日期转换为毫秒,然后获得时间差.
Calendar c1 = Calendar.getInstance();
c1.setTime(new SimpleDateFormat("yyyyMMddHHmmss").parse("20110327032913"));
System.out.println(c1.getTimeInMillis());
Calendar c2 = Calendar.getInstance();
c2.setTime(new SimpleDateFormat("yyyyMMddHHmmss").parse("20110327025913"));
System.out.println(c2.getTimeInMillis());
Run Code Online (Sandbox Code Playgroud)
结果:
1301189353000
1301191153000
显然,第一个日期晚于第二个日期,但其转换的毫秒数较小.我在格式上有任何错误吗?
我想Map<String, List<String>>在Java中实例化,
我试过了
Map<String, List<String>> foo = new <String, List<String>>();
Run Code Online (Sandbox Code Playgroud)
和
Map<String, List<String>> foo = new <String, ArrayList<String>>();
Run Code Online (Sandbox Code Playgroud)
他们都没有工作.有没有人知道如何在Java中实例化这个地图?
我有一个很大的问题 SerialPort.Open();
我正在与USB虚拟COM端口(cdc)进行通信,它被列为COM2.
它在TeraTerm/hyperTerminal等中工作正常.但是当我尝试在C#中打开端口时,它给了我异常The port 'COM2' does not exist.
我使用该SerialPort.GetPortNames()函数获取端口名称,并且在调试时它看起来很好.
我试图将名称设置为硬编码,但没有运气.
现在真的很奇怪,它在某些PC上工作正常,而在其他PC上则失败了.在某些PC上它会一直失败,而在其他PC上它会失败10%的时间.
更奇怪的是它取决于所使用的USB端口,一些端口工作正常,其他端口失败(在同一台PC上!).
请问有人帮帮我吗?
这是chrert的问题的后续问题Generic classes with Collection getter of other types.如果您能为我的问题想出更好的标题,请随时编辑它:
下面的代码包含一个GenericClass<T>带有返回类型方法的泛型类T和另一个带有返回类型的方法Collection<String>,它显然独立于T.
现在,如果我实例化一个raw GenericClass(我永远不会这样做,所以这个问题更像是一个理论问题,以帮助理解正在发生的事情)然后在增强的for循环中调用该方法将不起作用,因为所有泛型类型信息似乎在使用原始类型时迷路.但是,当在一个赋值中调用相同的方法时,它可以工作(它警告类型不安全,但它编译).
在我看来,要么两者都行不通,要么两者都行不通.我不明白为什么一个有效,另一个没有.你有任何提示,或知道JLS的任何部分解释这种行为?
public class GenericClass<T> {
T doSomething() {
return null;
}
Collection<String> getCollection() {
return Collections.emptyList();
}
public static void main(String[] args) {
GenericClass raw = new GenericClass();
// This will not compile (Error message below)
for (String str : raw.getCollection()) {
// Type mismatch: cannot convert from element type Object to String
}
// This …Run Code Online (Sandbox Code Playgroud) 我是nfc相关开发的新手.我想构建用于在mifare desfire ev1智能卡中读写安全元素的java应用程序.所以我找到了免费的库.但问题是我想首先验证智能卡.所以我创建了一些身份验证方法,并尝试使用它.但它总是说该类不支持error.in此代码我想加载一些键到application.i想知道首先我想创建一些应用程序,如果它想要的创建应用程序如何首先进行身份验证.
这些是我的代码
/**
* Load key
*/
public String loadKey(byte[] key, char keyType) throws CardException
{
String res = null;
System.out.println("Load Key");
CommandAPDU apdu = this.reader.loadKey(this.decoder.decode(key), keyType);
showAPDU(apdu.getBytes());
ResponseAPDU r = send(apdu);
showResponse(r.toString());
if (r.getSW() == E_NO_ERROR) {
System.out.println("Sucessfully Load keys");
} else{
System.out.println("load keys failed");
}
res = APDUtoJSON(r).toString();
return res;
}
//send command apdu
private ResponseAPDU send(CommandAPDU cmdApdu) throws CardException {
return card.getBasicChannel().transmit(cmdApdu);
}
// read response in main class i call these methods
byte[] key …Run Code Online (Sandbox Code Playgroud) 我使用以下方法在白色空格上分割字符串
myString.split("\\s+");
Run Code Online (Sandbox Code Playgroud)
我如何为单个空间提供例外.即在空间上分开,除了单个空间
我想在视图上用不同颜色绘制多条线并撤消,重做android中的路径.
我使用位图绘制选项,每个路径都有一个独特的颜色但撤消,重做不起作用..
这是我的bitmappaint代码:
public MyView(Context context, Object object) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
mPath = new Path();
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(0xFFFFFF00);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(3);
mBitmap = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
}
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
super.onSizeChanged(w, h, oldw, oldh);
}
protected void onDraw(Canvas canvas)
{
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
for (Path p : paths)
{
canvas.drawPath(p, mPaint);
}
canvas.drawPath(mPath, mPaint);
}
public boolean onTouchEvent(MotionEvent event)
{
float x …Run Code Online (Sandbox Code Playgroud) 我在c#中编写一个代码来排序一个数组,我希望右侧的所有负值和左侧的所有正值,不应该按降序排列
namespace SortApp
{
class Program
{
static void Main(string[] args)
{
int[] newInt = new int[] { 5, -2, -1, -4, -20, 6, 7, -14, 15, -16, 8, 9, 10 };
int size = 12, i= 0; // or newInt.Length
for (i = 0; i < newInt.Length; i++)
{
if (newInt[i] < 0 && newInt[size] > 0)
{
int temp = newInt[i];
newInt[i] = newInt[size];
newInt[size] = temp;
size--;
}
}
for (i = 0; i < newInt.Length; i++) …Run Code Online (Sandbox Code Playgroud) 我的代码中有一个String对象
String tempString = "Some String";
Run Code Online (Sandbox Code Playgroud)
现在,如果我写一些东西
tempString.toString();
Run Code Online (Sandbox Code Playgroud)
这会在String池中创建另一个String对象吗?