如果我运行以下程序,它解析引用时间间隔为1秒的两个日期字符串并比较它们:
public static void main(String[] args) throws ParseException {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str3 = "1927-12-31 23:54:07";
String str4 = "1927-12-31 23:54:08";
Date sDt3 = sf.parse(str3);
Date sDt4 = sf.parse(str4);
long ld3 = sDt3.getTime() /1000;
long ld4 = sDt4.getTime() /1000;
System.out.println(ld4-ld3);
}
Run Code Online (Sandbox Code Playgroud)
输出是:
353
为什么ld4-ld3不1(正如我所期望的那样,在时间上只有一秒钟的差异),但是353?
如果我将日期更改为1秒后的时间:
String str3 = "1927-12-31 23:54:08";
String str4 = "1927-12-31 23:54:09";
Run Code Online (Sandbox Code Playgroud)
然后ld4-ld3会1.
Java版本:
java version "1.6.0_22"
Java(TM) SE Runtime Environment (build 1.6.0_22-b04) …Run Code Online (Sandbox Code Playgroud) 假设我在类或方法中有一个泛型成员,所以:
public class Foo<T>
{
public List<T> Bar { get; set; }
public void Baz()
{
// get type of T
}
}
Run Code Online (Sandbox Code Playgroud)
当我实例化类时,T变为MyTypeObject1,所以类具有通用列表属性:List<MyTypeObject1>.这同样适用于非泛型类中的泛型方法:
public class Foo
{
public void Bar<T>()
{
var baz = new List<T>();
// get type of T
}
}
Run Code Online (Sandbox Code Playgroud)
我想知道,我的类列表包含什么类型的对象.所以调用的list属性Bar或局部变量baz包含什么类型的T?
我做不到Bar[0].GetType(),因为列表可能包含零元素.我该怎么做?
我的应用程序中有一些表单.
如何以这样的方式保护表单:如果有人导航或关闭浏览器选项卡,应提示他们确认他们确实要保留未保存数据的表单?
我想解析"3.5"一个双字符串的字符串.然而,
double.Parse("3.5")
Run Code Online (Sandbox Code Playgroud)
收益35和
double.Parse("3.5", System.Globalization.NumberStyles.AllowDecimalPoint)
Run Code Online (Sandbox Code Playgroud)
抛出一个FormatException.
现在我的计算机的语言环境设置为德语,其中逗号用作小数分隔符.它可能需要做些什么并double.Parse()期待"3,5"作为输入,但我不确定.
如何解析包含十进制数的字符串,该十进制数可能是也可能不是我当前语言环境中指定的格式?
如何将整数转换为二进制表示?
我正在使用此代码:
String input = "8";
String output = Convert.ToInt32(input, 2).ToString();
Run Code Online (Sandbox Code Playgroud)
但它引发了一个例外:
找不到任何可解析的数字
我正在尝试从操作方法重定向到外部URL但无法使其工作.有人可以解释我的错误吗?
public void ID(string id)
{
string url = string.Empty;
switch (id)
{
case "DB2FCB11-579F-4DA2-A68C-A6495B9BAAB5":
url = "http://www.somesite.com";
break;
}
Response.Redirect(url, true);
}
Run Code Online (Sandbox Code Playgroud)
谢谢,克里斯
我想在我的文件中附加行.我正在使用StreamWriter:
StreamWriter file2 = new StreamWriter(@"c:\file.txt");
file2.WriteLine(someString);
file2.Close();
Run Code Online (Sandbox Code Playgroud)
我的文件输出应该是几个字符串彼此相同,但我只有一行,每次运行此代码时都会被覆盖.
有没有办法让StreamWriter附加到现有文件?
在循环中使用break语句是不好的做法?for
说,我在数组中搜索一个值.比较for循环内部和找到值时,break;退出for循环.
这是一种不好的做法吗?我已经看到了使用替代:定义一个变量vFound,当被发现的价值它设置为true,并检查vFound在for语句条件.但是,是否有必要为此目的创建一个新变量?
我在正常的C或C++ for循环的上下文中询问.
PS:MISRA编码指南建议不要使用break.
在WebForms中,我通常会有这样的代码让浏览器显示一个"下载文件"弹出窗口,其中包含任意文件类型,如PDF和文件名:
Response.Clear()
Response.ClearHeaders()
''# Send the file to the output stream
Response.Buffer = True
Response.AddHeader("Content-Length", pdfData.Length.ToString())
Response.AddHeader("Content-Disposition", "attachment; filename= " & Server.HtmlEncode(filename))
''# Set the output stream to the correct content type (PDF).
Response.ContentType = "application/pdf"
''# Output the file
Response.BinaryWrite(pdfData)
''# Flushing the Response to display the serialized data
''# to the client browser.
Response.Flush()
Response.End()
Run Code Online (Sandbox Code Playgroud)
如何在ASP.NET MVC中完成相同的任务?
我的单元中的任何调用都会测试,Debug.Write(line)或者Console.Write(Line)只是在调试时跳过,并且从不打印输出.从我正在使用的类中调用这些函数.
我知道单元测试是自动化的,但我仍然希望能够从单元测试中输出消息.